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()
    {

    }
}

亲测有效!!!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Unity 2019.4.x中文文档是指为Unity 2019.4.x版本提供的中文版文档。这份文档是Unity官方提供的开发指南,旨在帮助开发人员使用Unity游戏引擎来创建游戏和应用程序。 Unity 2019.4.x中文文档覆盖了各个方面的内容,包括Unity界面的介绍,项目管理,场景编辑,游戏对象的创建与编辑,材质和纹理的应用,光照和阴影的处理,物理引擎的使用,动画的创建与控制,音频的处理,以及用户界面的设计等等。 这份文档提供了详细的说明和示例,以帮助开发人员了解和掌握各个功能和工具的使用方法。通过阅读文档,开发人员可以学习如何使用Unity的各种组件和系统来实现自己的游戏或应用程序的需求。 同时,Unity 2019.4.x中文文档也提供了大量的编程接口文档,涵盖了Unity中的各种类和函数的用法和说明。这些接口文档使开发人员能够更好地理解Unity引擎的内部结构和工作原理,以便更加高效和灵活地进行开发工作。 总之,Unity 2019.4.x中文文档是Unity官方提供的重要参考资料,对于想要使用Unity引擎进行游戏和应用程序开发的开发人员来说,是一份不可或缺的指南和学习资料。 ### 回答2: Unity 2019.4.x中提供了全面的中文文档支持,为用户提供了更方便、直观的学习和使用体验。 首先,Unity的中文文档涵盖了各个方面的内容,包括引擎的各个模块、功能的使用、编辑器的操作指南等。无论是初学者还是有一定经验的开发者,都能够在中文文档中找到自己需要的信息,帮助他们更好地了解和使用Unity。 其次,Unity的中文文档以详细的说明和示例来解释每个功能和概念。无论是脚本编程、场景编辑、粒子系统还是动画制作,中文文档中都会提供清晰的步骤和例子,帮助用户理解和掌握各种功能。 另外,Unity的中文文档还会根据官方版本进行及时的更新,保持与最新版本Unity保持同步。这意味着用户可以始终获得最新的特性和改进的详细解释,帮助他们更好地利用Unity的最新功能进行开发。 最后,Unity的中文文档还提供了丰富的教程和案例,使用户可以通过实际操作来学习。这些教程和案例涵盖了不同类型的游戏和应用开发,供用户参考和借鉴。用户可以通过这些实例来加深对Unity的理解,并且可以根据自己的实际需求进行修改和扩展。 总的来说,Unity 2019.4.x中文文档为用户提供了全面、详细的学习和使用指南,帮助他们更好地掌握Unity的各种功能和技术。这些文档的存在使得Unity成为了一个广受欢迎的开发工具,为用户创造了更好的开发环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小猴子编程

请支持一下我的分享

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

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

打赏作者

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

抵扣说明:

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

余额充值