unity之连接SQLite数据库

前端之家收集整理的这篇文章主要介绍了unity之连接SQLite数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
创建数据库,可以选择sqliteManager或者其他的sqlite工具,我创建了一个MyDataBase.sqlite,里面含有一个People表,内含2列,分别为name 和sex



为其添加2个字段


准备工作就做完了,下面就可以写代码

打开unity,把Mono.Data.sqlite文件和System.Data.dll放到Assets目录下,在unity5.1的版本还要额外导入一个sqlite3.dll文件

数据库有关的类

https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx
using UnityEngine;
using System.Collections;
using Mono.Data.sqlite;
using System.Data.sqlClient;

public class Mydata  {
     private sqliteConnection sqlConnection;
    private sqliteCommand sqlCommand;//数据库指令对象,也就是来执行sqlite语句的
    private sqliteDataReader sqlReader;//数据库读取对象,
    private string dataname = "Data Source = " + Application.dataPath + "/MyDataBase.sqlite";//你数据库所在的位置

    public Mydata() {

        sqlConnection = new sqliteConnection(dataname);
        sqlConnection.Open();
 
    
    }


    public  void OpenData(){
       sqlConnection = new sqliteConnection(dataname);
        sqlConnection.Open();
    
    }

    public void QueryAll(){
        OpenData();
        sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "SELECT * FROM People;";
        sqlCommand.ExecuteNonQuery();//执行语句
        sqlReader = sqlCommand.ExecuteReader();//读取
        ShowData();
        Close();
       
    
    }

    public void QueryOne() {
        OpenData();
        //查询name列,可能有多个值
        sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "Select  name from People;";
        sqlCommand.ExecuteNonQuery();
        sqlReader = sqlCommand.ExecuteReader();
        ShowData();
        Close();
        Debug.Log("**********************************");

    
    
    }

    public void QuerySpecalData() { 
    //查询满足条件的一个值
        OpenData();
        sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "select sex from People where name = 'cb';";
        sqlCommand.ExecuteNonQuery();
        sqlReader = sqlCommand.ExecuteReader();
        sqlReader.Read();
        Debug.Log(  sqlReader.GetValue(0));
        Close();
        Debug.Log("**********************************");
      
    
    
    }


    //插入
    public void InsertData() {
        OpenData();
        //Reader如果不管,就执行不了新的读取操作
        sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "insert into People values('bb','m');";
    
         int n =  sqlCommand.ExecuteNonQuery();
        Debug.Log(n);
    
        //一个sqliteCommand只能执行一条语句,如果想要在次执行,必须重新创建
        sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "select name  from People where sex = m;";
        sqlCommand.ExecuteNonQuery();
        sqlReader = sqlCommand.ExecuteReader();
        sqlReader.Read();
        Debug.Log(sqlReader.GetValue(0));
        Close();
        Debug.Log("**********************************");

    
    
    }


   public  void updateData() {
       OpenData();
       sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "update people  set name = 'mm' where name = 'bb';";
       int n = sqlCommand.ExecuteNonQuery();
       Debug.Log(n);
       sqlCommand = sqlConnection.CreateCommand();
       sqlCommand.CommandText = "select  name from People;";
       sqlCommand.ExecuteNonQuery();
       sqlReader = sqlCommand.ExecuteReader();
       ShowData();
       Close();
       Debug.Log("**********************************");
    
    }

   public void delectData() {
       OpenData();
       sqlCommand = sqlConnection.CreateCommand();
       sqlCommand.CommandText = "delete from People where name = 'wal';";
       sqlCommand.ExecuteNonQuery();

       sqlCommand = sqlConnection.CreateCommand();
       sqlCommand.CommandText = "select name from People;";
       sqlCommand.ExecuteNonQuery();
       sqlReader = sqlCommand.ExecuteReader();
      ShowData();
      Close();
      Debug.Log("**********************************");
   }


    public void ShowData() { 
    
    while(sqlReader.Read()){
        for (int i = 0; i < sqlReader.FieldCount; i++)
        {
            if (sqlReader.GetValue(i) == null)
            {
                Debug.Log("该列值为null");
            }
            else
            {
                Debug.Log(sqlReader.GetValue(i));
            }
        }
    
    
    }
    
    }

    public void Close() { 
    
    if(sqlReader!=null){

        sqlReader.Dispose();//释放所占用的资源
    }
        if(sqlCommand!=null){
            sqlCommand.Dispose();
        }

        if(sqlConnection!=null){
            sqlConnection.Close();//关闭数据库
        }
    
    }

	
}


Test(添加给照相机或随你喜欢)
using UnityEngine;
using System.Collections;

public class Test : MonoBehavIoUr {

    Mydata mydata;
	void Start () {
        mydata = new Mydata();
        mydata.QueryAll();
        mydata.QueryOne();
        mydata.QuerySpecalData();
        mydata.InsertData();
        mydata.updateData();
        mydata.delectData();
	}
	

	void Update () {
	
	}
}

注意:

1.sqliteReader,sqliteCommand用过一次必须要关,否则不能接受新的指令

2.sqlite不支持删除整列... 3.如果数据库的位置获取错误,可能出现找不到表的情况,先检查数据库里有没有这个表,再检查代码获取数据库所在位置的地方是否正确

原文链接:https://www.f2er.com/sqlite/199364.html

猜你在找的Sqlite相关文章