美文网首页
Unity中使用SQLite

Unity中使用SQLite

作者: _armour | 来源:发表于2019-06-17 18:02 被阅读0次

    准备环境

    Assets下新建Plugins文件夹,需要放入三个程序集:

    • Mono.Data.Sqlite.dll
    • System.Data.dll
    • sqlite3.dll

    前两个通过Unity安装目录下:
    Unity\Editor\Data\MonoBleedingEdge\lib\mono\对应.net版本
    拷贝Mono.Data.Sqlite.dll,System.Data.dll文件到Plugins

    第三个dll,进入sqlite下载地址【传送门
    找到 Precompiled Binaries for Windows
    下载对应位数的zip包,解压后,拷贝sqlite3.dllPlugins

    相关函数

    // db文件地址
    public static string dbPath = "data source = " + Application.dataPath + "test.db";
    // 建立连接
    SqliteConnection connection = new SqliteConnection(dbPath);
    connection.Open();
    // 实例化一个Command
    SqliteConnection command = connection.CreateCommand();
    // 赋值sql语句
    command.CommandText = "select * from data_role";
    // 执行读取数据
    SqliteDataReader reader = command.ExecuteReader();
    while (reader.Read()) {
        var id = reader.GetInt32(reader.GetOrdinal("id"));
        var name = reader.GetString(reader.GetOrdinal("name"));
        var power = reader.GetFloat(reader.GetOrdinal("power"));
        var desc = reader.GetString(reader.GetOrdinal("desc"));
        Debug.Log(string.Format("id = {0}, name = {1}, power = {2}, desc = {3}", id, name, power, desc));
    }
    // 关闭数据库
    reader.Close();
    command.Cancel();
    connection.Close();
    
    对应表格 执行结果

    各平台下数据库存储路径:

    • PC,iOS,Mac:"data source = " + Application.dataPath + "test.db";
    • Android:"URI = file:" + Application.dataPath + "test.db";

    相关文章

      网友评论

          本文标题:Unity中使用SQLite

          本文链接:https://www.haomeiwen.com/subject/hnqzfctx.html