unity SqLite读取行和列

unity SqLite读取行和列_第1张图片
unity SqLite读取行和列_第2张图片
项目文件
链接: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;
    #region
    public 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
    /// 
    /// 打开数据库
    /// 
    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);
        }

    }
    /// 
    /// 关闭数据库
    /// 
    void CloseConnect()
    {
        m_SqliteConnection.Close();
        m_SqliteConnection = null;
        m_SqliteCommand.Dispose();
        m_SqliteCommand = null;
    }
    /// 
    /// 不同平台地址
    /// 
    /// 
    /// 
    public string GetDataPath(string databasePath)
    {
#if UNITY_EDITOR
        return string.Concat("data source=", Application.streamingAssetsPath, "/", databasePath);
#endif
#if UNITY_ANDROID
            return string.Concat("URI=file:", Application.persistentDataPath, "/", databasePath);
#endif
#if UNITY_IOS
            return string.Concat("data source=", Application.persistentDataPath, "/", databasePath);
#endif
    }


    /// 
    /// 读取整张表
    /// 
    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));
            }
        }
    }
    /// 
    /// 读取行
    /// 
    /// 通过那列的命名来读取行
    /// 读取那行的名字
    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;
    }
    /// 
    /// 读取列
    /// 
    /// 
    /// 行的条件值
    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();
    }
    /// 
    /// 执行SQL语句
    /// 
    /// 
    /// 
    public SqliteDataReader ExecuteReader(string command)
    {
#if UNITY_EDITOR
        Debug.Log("SQL:ExecuteReader " + command);
#endif
        m_SqliteCommand.CommandText = command;
        m_SqliteDataReader = m_SqliteCommand.ExecuteReader();
        return m_SqliteDataReader;
    }
}

unity SqLite读取行和列_第3张图片
unity SqLite读取行和列_第4张图片

你可能感兴趣的:(unity,sqlite,游戏引擎)