Unity3D使用SQLite

前端之家收集整理的这篇文章主要介绍了Unity3D使用SQLite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

所需dll放到Plugins目录下

DbAccess.cs

using UnityEngine;
using System;
using System.Collections;
using Mono.Data.sqlite;

public class DbAccess
{

    private sqliteConnection dbConnection;

    private sqliteCommand dbCommand;

    private sqliteDataReader reader;

    public DbAccess(string connectionString)
    {

        OpenDB(connectionString);

    }
    public DbAccess()
    {

    }
    /// <summary>
    /// 打开数据库
    /// </summary>
    /// <param name="connectionString"></param>
    public void OpenDB(string connectionString)
    {
        try
        {
            dbConnection = new sqliteConnection(connectionString);

            dbConnection.Open();

            Debug.Log("Connected to db");
        }
        catch (Exception e)
        {
            string temp1 = e.ToString();
            Debug.Log(temp1);
        }

    }
    /// <summary>
    /// 关闭连接
    /// </summary>
    public void ClosesqlConnection()
    {

        if (dbCommand != null)
        {

            dbCommand.Dispose();

        }

        dbCommand = null;

        if (reader != null)
        {

            reader.Dispose();

        }

        reader = null;

        if (dbConnection != null)
        {

            dbConnection.Close();

        }

        dbConnection = null;

        Debug.Log("Disconnected from db.");

    }

    public sqliteDataReader ExecuteQuery(string sqlQuery)
    {
        try
        {
            dbCommand = (sqliteCommand)dbConnection.CreateCommand();

            dbCommand.CommandText = sqlQuery;

            reader = dbCommand.ExecuteReader();
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }
            return reader;
        
    }
    /// <summary>
    /// 读取整个表
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public sqliteDataReader ReadFullTable(string tableName)
    {

        string query = "SELECT * FROM " + tableName;

        return ExecuteQuery(query);

    }
    /// <summary>
    /// 插入一条数据,表名,对应的条目值
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="values"></param>
    /// <returns></returns>
    public sqliteDataReader InsertInto(string tableName,string[] values)
    {

        string query = "INSERT INTO " + tableName + " VALUES (" + values[0];

        for (int i = 1; i < values.Length; ++i)
        {

            query += "," + values[i];

        }

        query += ")";

        return ExecuteQuery(query);

    }
    /// <summary>
    /// 更新某条信息,表名,item name,value,key,value
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="cols"></param>
    /// <param name="colsvalues"></param>
    /// <param name="selectkey"></param>
    /// <param name="selectvalue"></param>
    /// <returns></returns>
    public sqliteDataReader UpdateInto(string tableName,string[] cols,string[] colsvalues,string selectkey,string selectvalue)
    {

        string query = "UPDATE " + tableName + " SET " + cols[0] + " = " + colsvalues[0];

        for (int i = 1; i < colsvalues.Length; ++i)
        {

            query += "," + cols[i] + " =" + colsvalues[i];
        }

        query += " WHERE " + selectkey + " = " + selectvalue + " ";

        return ExecuteQuery(query);
    }
    /// <summary>
    /// DELETE FROM table WHERE key=value
    /// </summary>
    /// <param name="tableName"></param>
    /// <param name="cols"></param>
    /// <param name="colsvalues"></param>
    /// <returns></returns>
    public sqliteDataReader Delete(string tableName,string[] colsvalues)
    {
        string query = "DELETE FROM " + tableName + " WHERE " + cols[0] + " = " + colsvalues[0];

        for (int i = 1; i < colsvalues.Length; ++i)
        {

            query += " or " + cols[i] + " = " + colsvalues[i];
        }
        Debug.Log(query);
        return ExecuteQuery(query);
    }

    public sqliteDataReader InsertIntoSpecific(string tableName,string[] values)
    {

        if (cols.Length != values.Length)
        {

            throw new sqliteException("columns.Length != values.Length");

        }

        string query = "INSERT INTO " + tableName + "(" + cols[0];

        for (int i = 1; i < cols.Length; ++i)
        {

            query += "," + cols[i];

        }

        query += ") VALUES (" + values[0];

        for (int i = 1; i < values.Length; ++i)
        {

            query += "," + values[i];

        }

        query += ")";

        return ExecuteQuery(query);

    }

    public sqliteDataReader DeleteContents(string tableName)
    {

        string query = "DELETE FROM " + tableName;

        return ExecuteQuery(query);

    }
    /// <summary>
    /// 创建表
    /// </summary>
    /// <param name="name"></param>
    /// <param name="col"></param>
    /// <param name="colType"></param>
    /// <returns></returns>
    public sqliteDataReader CreateTable(string name,string[] col,string[] colType)
    {

        if (col.Length != colType.Length)
        {

            throw new sqliteException("columns.Length != colType.Length");

        }

        string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];

        for (int i = 1; i < col.Length; ++i)
        {

            query += "," + col[i] + " " + colType[i];

        }

        query += ")";

        return ExecuteQuery(query);

    }

    public sqliteDataReader SelectWhere(string tableName,string[] items,string[] operation,string[] values)
    {

        if (col.Length != operation.Length || operation.Length != values.Length)
        {

            throw new sqliteException("col.Length != operation.Length != values.Length");

        }

        string query = "SELECT " + items[0];

        for (int i = 1; i < items.Length; ++i)
        {

            query += "," + items[i];

        }

        query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";

        for (int i = 1; i < col.Length; ++i)
        {

            query += " AND " + col[i] + operation[i] + "'" + values[i] + "' ";

        }

        return ExecuteQuery(query);

    }

}

原文地址 原文链接:https://www.f2er.com/sqlite/200761.html

猜你在找的Sqlite相关文章