unity 2019 3.X版本使用sqlite

写一个unity 使用sqlite存储数据。

当前使用unity版本:2019.3.5。

一开始找了很多的资料,需要Mono.Data.Sqlite.dll,System.Data.dll,Sqlite3.dll3个dll.但是我当前的版本不需要System.Data.dll。而且在当前版本的mono文件下找到的Mono.Data.Sqlite.dll,使用起来会报错。最后我去2018的unity安装目录下找了Mono.Data.Sqlite.dll(Editor\Data\Mono\lib\mono\2.0\ Mono.Data.Sqlite.dll)就不报错了。其他大神关于unity 使用sqlite教程

sqlite.dll下载 x86 和 x64 架构

之后将dll放入到assets/pludins文件夹下。

建立数据库,我将数据库放在了streamingAssets下

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

public class SQLite
{

    public SqliteConnection connection;
    private SqliteCommand command;


    public SQLite(string path)
    {
        try
        {
            connection = new SqliteConnection(path);    // 创建SQLite对象的同时,创建SqliteConnection对象
            connection.Open();                          // 打开数据库链接
            Debug.Log("Connect successful!");
            Command();
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
        }
      
    }


    public SqliteCommand Command()
    {
        command = connection.CreateCommand();
        return command;
    }

    // 【增加数据】
    public SqliteDataReader InsertData(string table_name, string[] fieldNames, object[] values)
    {
        // 如果字段的个数,和数据的个数不相等,无法执行插入的语句,所以返回一个null
        if (fieldNames.Length != values.Length)
        {
            return null;
        }

        Debug.Log("table_name:" + table_name);
        command.CommandText = "insert into " + table_name + "(";
        
        for (int i = 0; i < fieldNames.Length; i++)
        {
            command.CommandText += fieldNames[i];
            if (i < fieldNames.Length - 1)
            {
                command.CommandText += ",";
            }
        }

        command.CommandText += ")" + "values (";

        for (int i = 0; i < values.Length; i++)
        {
            command.CommandText += values[i];

            if (i < values.Length - 1)
            {
                command.CommandText += ",";
            }
        }

        command.CommandText += ")";

        Debug.Log(command.CommandText);

        return command.ExecuteReader();

    }


    // 【删除数据】
    public SqliteDataReader DeleteData(string table_name, string[] conditions)
    {
        command.CommandText = "delete from " + table_name + " where " + conditions[0];

        for (int i = 1; i < conditions.Length; i++)
        {
            // or:表示或者
            // and:表示并且
            command.CommandText += " or " + conditions[i];
        }

        return command.ExecuteReader();
    }

    // 【修改数据】

    public SqliteDataReader UpdateData(string table_name, string[] values, string[] conditions)
    {
        command.CommandText = "update " + table_name + " set " + values[0];

        for (int i = 1; i < values.Length; i++)
        {
            command.CommandText += "," + values[i];
        }

        command.CommandText += " where " + conditions[0];
        for (int i = 1; i < conditions.Length; i++)
        {
            command.CommandText += " or " + conditions[i];
        }

        return command.ExecuteReader();
    }

    // 【查询数据】
    public SqliteDataReader SelectData(string table_name, string[] fields)
    {
        command.CommandText = "select " + fields[0];
        for (int i = 1; i < fields.Length; i++)
        {
            command.CommandText += "," + fields[i];
        }
        command.CommandText += " from " + table_name;

        return command.ExecuteReader();
    }


    // 【查询整张表的数据】
    public SqliteDataReader SelectFullTableData(string table_name)
    {
        command.CommandText = "select * from " + table_name;
        return command.ExecuteReader();
    }


    // 【关闭数据库】
    public void CloseDataBase()
    {
        connection.Close();
        command.Cancel();
    }

}

 这里注意 连接数据库路径//需带'Data Source='前缀

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

public class Test : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        // 数据库文件的具体路径,有的是.sqlite/.db
        string path = "Data Source="+Application.streamingAssetsPath + "/" + "settingdata.db";
        Debug.Log("path:" + path);

        SQLite sql = new SQLite(path);

        SqliteDataReader reader1 = sql.InsertData("ddd", new string[] { "name", "score" }, new object[] { "'Sivan'", 99 });
        // 读取到的信息使用之后需要关闭
        reader1.Close();

        sql.CloseDataBase();
    }

    // Update is called once per frame
    void Update()
    {

    }
}

亲测有效!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猴子编程

请支持一下我的分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值