1.所需配置文件
其中 Mono.Data.Sqlite 和System.Data要和你的unity版本相对应,可以在 C:\ Program Files(x86)\ Unity \ Editor \ Data \ Mono \ lib \ mono \ 2.0 *中复制
sqlite3.dll可以在这里下载这些文件http://www.sqlite.org/download.html for Windows
2.注意事项
这个地方要设置成.NET 2.0,不然打包会出错
3.测试脚本
using UnityEngine;
uusing UnityEngine;
using System.Collections;
using System.IO;
using Mono.Data.Sqlite;
using UnityEngine.UI;
public class SQLiteDemo : MonoBehaviour
{
///
/// SQLite数据库辅助类
///
private SQLiteHelper sql;
private string q;
private string qer;
private string qere;
private string qdf;
public Text text;
void Start()
{
string path = "data source=" + Application.streamingAssetsPath + "/DataBase.db";
print("kaishi");
text.text = "kaishi";
try
{
FindDataBase();
while (sql==null)
{
}
}
catch (System.Exception e)
{
text.text = e.Message;
throw;
}
//创建名为sqlite4unity的数据库
if (sql == null)
{
Debug.LogError("sql不能为空");
}
// 创建名为table1的数据表
sql.CreateTable("table1", new string[] { "ID", "Name", "Age", "Email" }, new string[] { "INTEGER", "TEXT", "INTEGER", "TEXT" });
//插入两条数据
sql.InsertValues("table1", new string[] { "'1'", "'张三'", "'22'", "'[email protected]'" });
sql.InsertValues("table1", new string[] { "'2'", "'李四'", "'25'", "'[email protected]'" });
// 更新数据,将Name = "张三"的记录中的Name改为"Zhang3"
sql.UpdateValues("table1", new string[] { "Name" }, new string[] { "'Zhang3'" }, "Name", "=", "'张三'");
// 插入3条数据
sql.InsertValues("table1", new string[] { "3", "'王五'", "25", "'[email protected]'" });
sql.InsertValues("table1", new string[] { "4", "'王五'", "26", "'[email protected]'" });
sql.InsertValues("table1", new string[] { "5", "'王五'", "27", "'[email protected]'" });
//删除Name = "王五"且Age = 26的记录,DeleteValuesOR方法类似
sql.DeleteValuesAND("table1", new string[] { "Name", "Age" }, new string[] { "=", "=" }, new string[] { "'王五'", "'26'" });
// 读取整张表
SqliteDataReader reader = sql.ReadFullTable("table1");
while (reader.Read())
{
q = reader.GetInt32(reader.GetOrdinal("ID")).ToString ();
//读取ID
Debug.Log(reader.GetInt32(reader.GetOrdinal("ID")));
//读取Name
Debug.Log(reader.GetString(reader.GetOrdinal("Name")));
//读取Age
Debug.Log(reader.GetInt32(reader.GetOrdinal("Age")));
//读取Email
Debug.Log(reader.GetString(reader.GetOrdinal("Email")));
text.text+=q;
}
//读取数据表中Age>=25的所有记录的ID和Name
reader = sql.ReadTable("table1", new string[] { "ID", "Name" }, new string[] { "Age" }, new string[] { ">=" }, new string[] { "'25'" });
while (reader.Read())
{
//读取ID
Debug.Log(reader.GetInt32(reader.GetOrdinal("ID")));
//读取Name
Debug.Log(reader.GetString(reader.GetOrdinal("Name")));
}
//自定义SQL,删除数据表中所有Name="王五"的记录
sql.ExecuteQuery("DELETE FROM table1 WHERE NAME='王五'");
//关闭数据库连接
sql.CloseConnection();
}
public Text t;
// Use this for initialization
void FindDataBase()
{
t.text = "数据库内容\r\n";
string appDBPath = Application.persistentDataPath + "/" + "DataBase.db";
//t.text += Application.persistentDataPath + "\r\n";
#if UNITY_EDITOR
LoadData(Application.dataPath + "/StreamingAssets/DataBase.db");
#elif UNITY_ANDROID
if (!File.Exists(appDBPath))
{
StartCoroutine(LoadDatabase(appDBPath));
}
else
{
LoadData(appDBPath);
}
#endif
}
IEnumerator LoadDatabase(string path)
{
string dataPath = "";
#if UNITY_ANDROID
dataPath = "jar:file://" + Application.dataPath + "!/assets/" + "DataBase.db";
#endif
WWW loadDB = new WWW(dataPath);
yield return loadDB;
if (loadDB.isDone)
{
Debug.Log(loadDB.bytes.Length);
File.WriteAllBytes(path, loadDB.bytes);
LoadData(path);
}
}
private void LoadData(string path)
{
sql = new SQLiteHelper(path);
}
}
4.可以使用可视化工具 SQLiteStudio查看编辑db数据库 下载地址链接:http://pan.baidu.com/s/1qYocI9u 密码:mk45