unity 2019 3.X版本使用sqlite

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

}
}

  

posted @ 2022-02-09 17:29  多见多闻  阅读(223)  评论(0)    收藏  举报