项目文件
链接:https://pan.baidu.com/s/1BabHvQ-y0kX_w15r7UvIGQ
提取码:emsg
–来自百度网盘超级会员V6的分享
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
using System;
using System.Data;
using System.Reflection;public class ConnectSQL : MonoBehaviour
{public SqliteConnection m_SqliteConnection;public SqliteCommand m_SqliteCommand;public SqliteDataReader m_SqliteDataReader;private string m_data;private bool m_IsOpen;#regionpublic void Start(){OpenDB();}private void OnDestroy(){CloseConnect();}private void Update(){if(Input.GetKeyDown(KeyCode.Space)){if(m_IsOpen){Select("img", "name");// SelectColl("img", "qqq");}}}#endregion/// <summary>/// 打开数据库/// </summary>private void OpenDB(){try{string file = GetDataPath("test.db");m_SqliteConnection = new SqliteConnection(file);m_SqliteConnection.Open();m_IsOpen = true;m_SqliteCommand = m_SqliteConnection.CreateCommand();Debug.Log("打开成功");// ReadData();}catch (Exception e){Debug.LogError(e.Message);}}/// <summary>/// 关闭数据库/// </summary>void CloseConnect(){m_SqliteConnection.Close();m_SqliteConnection = null;m_SqliteCommand.Dispose();m_SqliteCommand = null;}/// <summary>/// 不同平台地址/// </summary>/// <param name="databasePath"></param>/// <returns></returns>public string GetDataPath(string databasePath){
#if UNITY_EDITORreturn string.Concat("data source=", Application.streamingAssetsPath, "/", databasePath);
#endif
#if UNITY_ANDROIDreturn string.Concat("URI=file:", Application.persistentDataPath, "/", databasePath);
#endif
#if UNITY_IOSreturn string.Concat("data source=", Application.persistentDataPath, "/", databasePath);
#endif}/// <summary>/// 读取整张表/// </summary>public void ReadData(){string sqlQuery = "SELECT * FROM img"; m_SqliteDataReader = ExecuteReader(sqlQuery);while (m_SqliteDataReader.Read()){for (int i = 0; i < m_SqliteDataReader.FieldCount; i++){Debug.Log(m_SqliteDataReader.GetValue(i));}}}/// <summary>/// 读取行/// </summary>/// <param name="tableName">通过那列的命名来读取行</param>/// <param name="coll_value">读取那行的名字</param>public void SelectColl(string tableName, string coll_value){string sql = "SELECT * FROM " + tableName + " WHERE name=" + "'" + coll_value + "'";//img是表名// string sql = "SELECT * FROM img WHERE name='asdad'";SqliteCommand sqliteCommand = new SqliteCommand(sql, m_SqliteConnection);SqliteDataReader sqliteDataReader = sqliteCommand.ExecuteReader();while (sqliteDataReader.Read()){for (int i = 0; i < sqliteDataReader.FieldCount; i++){m_data += sqliteDataReader.GetValue(i)+",";}}Debug.Log(m_data);//接收到数据列入数据队// m_MySqlData.Enqueue(data);m_data = "";sqliteDataReader.Close();sqliteCommand.Dispose();sqliteDataReader = null;sqliteCommand = null;}/// <summary>/// 读取列/// </summary>/// <param name="tableName"></param>/// <param name="column_name">行的条件值</param>public void Select(string tableName, string column_name){string sql = "SELECT " + column_name + " FROM " + tableName;SqliteCommand cmd = new SqliteCommand(sql, m_SqliteConnection);SqliteDataReader reader = cmd.ExecuteReader();while (reader.Read()){for (int i = 0; i < reader.FieldCount; i++){Debug.Log(reader.GetValue(i));} }cmd.Dispose();reader.Close();}/// <summary>/// 执行SQL语句/// </summary>/// <param name="command"></param>/// <returns></returns>public SqliteDataReader ExecuteReader(string command){
#if UNITY_EDITORDebug.Log("SQL:ExecuteReader " + command);
#endifm_SqliteCommand.CommandText = command;m_SqliteDataReader = m_SqliteCommand.ExecuteReader();return m_SqliteDataReader;}
}