Unity 数据库的简单使用

来源:互联网 发布:阿里大数据查询 编辑:程序博客网 时间:2024/05/18 02:36
下载地址:http://www.sqlite.org/download.html  将Precompiled Binaries for Windows下的包下载下来sqlite-dll-win64-x64-3150100.zip、sqlite-tools-win32-x86-3150100.zip  sqlite-dll-win64-x64-3150100.zip包含.def、.dll两个文件sqlite-tools-win32-x86-3150100.zip包含三个执行文件exe将它们一起解压到D:\sqlite文件夹,配置环境变量PATH后追加“D:\sqlite;”

1.创建一个Unity工程,创建一个文件夹命名为Plugins
2.导入两个文件到Plugins文件夹(Mono.Data.Sqlite.dll和System.Data.dll)
3.新建一个脚本用来操作数据库的使用
4.在脚本中引用命名空间Mono.Data.Sqlite(能引用证明成功了)

using System.Collections;using System.Collections.Generic;using UnityEngine;using Mono.Data.Sqlite;public class SqliteTest : MonoBehaviour {    void Start () {        //打开数据库        SqliteConnection con = new SqliteConnection("data source=" + Application.dataPath + "/data.db");        con.Open();        //创建表        SqliteCommand cmd = new SqliteCommand("create table if not exists User(uid integer, name text)",con);        //执行创建表命令        cmd.ExecuteNonQuery();        cmd.Dispose();        //插入一条数据        cmd = new SqliteCommand("insert into User values(1,'令狐冲')", con);        //执行插入数据命令        cmd.ExecuteNonQuery();        cmd.Dispose();        //查询当前几条结果        cmd = new SqliteCommand("select count(*) from User", con);        object obj = cmd.ExecuteScalar();        int count = System.Convert.ToInt32(obj);        Debug.Log("count:" + count);        cmd.Dispose();        //查询数据        cmd = new SqliteCommand("select * from User",con);        SqliteDataReader reader = cmd.ExecuteReader();        /*         1 令狐冲         2 岳不群         3 任盈盈         */        while (reader.Read())        {            //取出第一列            int uid = reader.GetInt32(0);            //取出第二列            string name = reader.GetString(1);            Debug.Log(uid + " " + name);        }        cmd.Dispose();        reader.Close();        con.Close();    }    void test()    {        SqliteManager.Instance.Open(Application.dataPath + "/data.db");        //创建表        SqliteManager.Instance.ExecuteNonQuery("create table if not exists User(uid integer, name text)");        //插入一条数据        SqliteManager.Instance.ExecuteNonQuery("insert into User values(2,'岳不群')");        //查询        SqliteDataReader reader = SqliteManager.Instance.ExecuteReader("select * from User");        //....        reader.Close();        SqliteManager.Instance.Close();    }}

其中大家看到了SqliteManager的单例所以我们还需要创建一个管理脚本

using System.Collections;using System.Collections.Generic;using UnityEngine;using Mono.Data.Sqlite;public class SqliteManager : MonoBehaviour {    public static SqliteManager Instance;    SqliteConnection con;    SqliteCommand cmd;    SqliteDataReader reader;    void Awake () {        Instance = this;    }    //打开数据库    public void Open(string fileName)    {        con = new SqliteConnection("data source=" + fileName);        con.Open();    }    //关闭数据库    public void Close()    {        con.Close();    }    //执行增删改    public int ExecuteNonQuery(string command)    {        cmd = new SqliteCommand(command, con);        int count = cmd.ExecuteNonQuery();        cmd.Dispose();        return count;    }    //执行查    public SqliteDataReader ExecuteReader(string command)    {        cmd = new SqliteCommand(command, con);        reader = cmd.ExecuteReader();        cmd.Dispose();        return reader;    }}

这样就实现了数据库的增删改查操作了 不喜勿喷 感谢支持