关于SqLite的研究

  做移动开发的时候,很多情况会用到数据库,大型的数据库也不能用,而sqlite则是以小巧著称。非常适合移动端的使用,当然要是联网在线的直接连接到服务器上又另当别算了。

那么,具体怎么使用呢?

引入三个文件(这里可以打包下次重复使用)

unity3d有自己对应的sqlite.dll分别需要三个文件
1.Mono.Data.Sqlite.dll 
在unity安装文件“Unity\Editor\Data\MonoBleedingEdge\lib\mono”可以找到,注意mono文件夹下面 有对应版本号,可以根据自己的项目来决定选择。

2.System.Data.dll   同上位置一样可以找到 ,不过建议使用2.0版本

3.sqlite3.dll   就在\Unity\Editor下可以找到

除此之外,还需要把这3个文件放在你的项目的这个路径下面:\Assets\Plugins\,没有Plugins文件夹就必须创建这个文件夹,然后将这三个dll文件放在该文件夹下面。当然,如果你想能够在PC上面发布成可执行文件,还需要改动一些地方。在unity3d中的Play Setting ->Other Setting 中将Api Compatibility的等级改为.NET 2.0;那么这些操作做完了以后,如果你的代码写得没有问题,那么你就可以成功了。

代码操作

首先引入
   using Mono.Data.Sqlite;
创建数据库
  private void CreateDB() {
        string connectionString = " Data Source = " + Application.dataPath + "/afterDB.sqlite" ;     
        dbConnection = new SqliteConnection (connectionString);
        dbConnection.Open();
     }

创建表

private void CreateTable()
    {
        //创建数据指令对象
        SqliteCommand command = new SqliteCommand(dbConnection);
        //给指令添加sql语句 :如果不存在person,创建; if exists是如果存在
        command.CommandText = "create table if not exists afterDB(name text,x float,y float,z float)";
        //执行sql 语句
        command.ExecuteNonQuery();
    }

这样就可进行增删改查的操作了。

这里用一个例子说明使用:

using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;

public class dbaccess  {
    public dbaccess(string dbName) {//构造方法
        OpenDB(dbName);
    }
    //开启和关闭数据库时用
    private SqliteConnection dbConnection;
    //执行命令语句时调用
    private SqliteCommand dbCommand;
    //执行命令语句都会返回一个SqlitDataReader
    private SqliteDataReader reader;
    //开启
    public void OpenDB( string DBName) {
        //捕获异常
        try
        {
            dbConnection = new SqliteConnection(DBName);
            dbConnection.Open();
            Debug.Log("db is open");
        }
        catch (UnityException ex)
        {
            Debug.Log(ex.ToString());
        }
    }
    //关闭
    public void CloseDb() {
        if (reader!=null)
        {
            //释放
            reader.Dispose();
        }
        reader = null;
        if (dbCommand != null)
        {
            //释放
            dbCommand.Dispose();
        }
        dbCommand = null;
        if (dbConnection != null)
        {
            //释放
            dbConnection.Close();
        }
        dbConnection = null;
        Debug.Log("db is close");
    }

    //执行命令语句的方法
    public SqliteDataReader ExcuteSQL(string sqlstr) {
        dbCommand = dbConnection.CreateCommand();
        dbCommand.CommandText = sqlstr;
        reader = dbCommand.ExecuteReader();
        return reader;
    }
    //创建表
    public SqliteDataReader CreateTable(string tableName,string[] colNames,string[] colTypes) {
        if (colNames.Length!=colTypes.Length)
        {
            throw new SqliteException("colNames.Length!=colTypes.Length");
        }
        string createTable = "create table " + tableName + "(" + colNames[0] + " " + colTypes[0];
        for (int i = 1; i < colNames.Length; i++)
        {
            createTable += "," + colNames[i] + " " + colTypes[i];
        }
        createTable += ")";
        return ExcuteSQL(createTable);
    }

    //增
    public SqliteDataReader IsertInto(string tableName,string[] values) 
    {
        string insertInto = "insert into " + tableName + " values (" + values[0];
        for (int i = 1; i < values.Length; i++)
        {
            insertInto += "," + values[i];
        }
        insertInto += ")";
        return ExcuteSQL(insertInto);
    }

    //删
    public SqliteDataReader Delete(string tableName,string[] colNames,string[] colValues)
    {
        string delete = "delete from " + tableName + " where " + colNames[0] + "=" + colValues[0];
        for (int i = 1; i < colNames.Length; i++)
        {
            delete += " or " + colNames[i] + "=" + colValues[i];
        }
        Debug.Log(delete);
        return ExcuteSQL(delete);
    }

    //改
    public SqliteDataReader Update(string tableName, string[] names, string[] values, string name, string value) 
    {
        string updateTable = "update " + tableName + "  set " + names[0] + "=" + values[0];
        for (int i = 1; i < names.Length; i++)
        {
            updateTable += "," + names[i] + "=" + values[i];
        }
        updateTable += " where " + name + "=" + value;
        return ExcuteSQL(updateTable);
    }

    //查
    public SqliteDataReader SelectTable(string tableName) {

        string selectTable = "select * from " + tableName;
        return ExcuteSQL(selectTable);
    }
}
在写一个调用这个脚本的脚本,挂在一个对象上;
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;

public class DBtest : MonoBehaviour {

	
	void Start () {
        dbaccess db = new dbaccess("data source=ghh.sqlite");
        //db.CreateTable("lc",new string[]{"name","sex","age"},new string[]{"text","text","integer"});

        //db.IsertInto("lc", new string[] { "'liuchao'", "'lan'", "23" });
        //db.IsertInto("lc", new string[] { "'liuchao'", "'lv'", "21" });
        //db.IsertInto("lc", new string[] { "'刘刚'", "'男'", "18" });

        //db.Delete("lc", new string[] { "name", "sex"}, new string[] { "'liuchao'", "'lv'" });
        //db.Update("lc", new string[] { "sex", "age" }, new string[] { "'lan'", "1213" },"name","liuchao");


        SqliteDataReader select = db.SelectTable("lc");
        while (select.Read())
        {
            string name = select.GetString(select.GetOrdinal("name"));
            Debug.Log(name);
           string sex = select.GetString(select.GetOrdinal("sex"));
            Debug.Log(sex);
            int age = select.GetInt32(select.GetOrdinal("age"));
           Debug.Log(age);
        }
 
    }
	
	
	void Update () {
	
	}
}

这样就完成了对数据库的各种操作了。。

这个从数据库读取数据需要注意一下

定义一个路径下的数据库我们这么写
   // 创建数据库对象
        connection = new SqliteConnection (con);
        // 打开数据库
        connection.Open();
我们读取数据的时候
private void SelectObject() {
        SqliteCommand command = new SqliteCommand(connection);
        command.CommandText = "select *from person where name=' 小明'";

        //第一行第一列
        object ob = command.ExecuteScalar();// 主要是这个查询
        print(ob);

        //读取数据
        SqliteDataReader reader = command.ExecuteReader();
        //每read 一下读一下
        while (reader.Read()) {
            print(reader[ "name"]);
            print(reader[ "num"]);
            print(reader[ "age"]);
        }

命令对象command调用的ExcuteScalar查询,而且还调用了ExecuteReader。这个需要注意的。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值