Unity&Sqlite数据库

  Sqlite是一个跨平台关系型小型数据库,非常便利,适合于嵌入式设备;对于Sqlite数据库来说,这个数据库是以文件的形成存在的(比如data.db);数据库是由表组成的,在一个数据库里面可以存储多个表,多个表之间往往存在某种关系,

  对于一个表的操作:增删改查,语句和SQLServer语句一样;在表中,有主键(不能为空,也不能重复,可以添加自增功能)、外键(和别的表有关联)、唯一键(unique可以为空,不能重复)。

  在控制台中,使用Sqlite的语句如下:

  sqlite3 data.db ; //打开数据库,没有的话创建一个

  .table ; // 查看数据库中有几个表

  creat table USER(uid integer, name text, score integer);  //创建表 表中有三个字段(可以不写类型,没有类型既什么类型都可以)

  insert into USER values(1,'郭靖',89)  //在USER表中插入一条新的数据

  select * from USER;  //查看当前表中的所有内容

  drop table USER;  //删除USER表

  create table if not exists USER(uid integer primary key autoincrement, name text, score integer);  //整型的uid自增

  insert into USER(name, score) values ('杨过',99);  //uid自增添加数据

  update USER set name='黄老邪' where score=89;  //修改score为89的人为黄老邪

  delete from USER;  //删除所有数据

  delete from USER where uid =1;  //删除表中uid为1的数据

select uid,name,score from USER;  //查找表中的内容

select name from USER;

select count(*) from USER;  //查找USER表中有几行数据

select sun(score) from USER;  //查找表中所有score的和

select avg(score) from USER;  //查找score的平均数

select * from USER where score>90 and score<95;  //查找表中score大于90且小于95的数据

select * from USER limit 2;  //查找现在前两条

select * form USER order by score  //根据score的大小顺序排序

select *from USER order by score decs;  //根据score的大小倒序排序

select USER.name, USER.score,KUNGFU.name from USER,KUNGFU where USER.uid=KUNGFU.uid;  //联合查询

.exit;  //退出

 

Sqlite在Unity中使用,需要先在Project->Assest中创建一个Plugins的文件夹,然后把数据库添加进去

代码如下:

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

public class SqliteText : MonoBehaviour {

	SqliteConnection con;	//数据库连接类

	void Start () {
		//连接数据库,如果没有则创建一个数据库
		con = new SqliteConnection ("Data Source =" + Application.database + "/Data/data.db");
		con.Open();//打开数据库
		//创建表
		string sqlStr="create table if not exist USER(uid integer primary key autoincrement, name text, score integer)";
		SqliteCommand command =new SqliteCommand(sqlStr,con);
		//执行命令(没有查询,适用于增删改)
		command.ExecuteNonQuery ();
		//关闭命令(因为Sqlite是单线程的,所以每次执行命令结束后,都应该关闭命令)
		command.Dispose ();
		//插入数据
		sqlStr = "insert into USER(name,score) values ('王大锤',88)";
		command.ExecuteNonQuery(sqlStr,con);
		command.Dispose ();
		sqlStr = "select count(*) from USER";
		//查询单个结果,并转化成整型
		int counts = Convert.ToInt32(command.ExecuteScalar ());
		command.Dispose ();

		//查询多个结果  SqliteDataReader 读取结果类
		sqlStr = "select * from USER";
		command = new SqliteCommand (sqlStr, con);
		SqliteDataReader reader = command.ExecuteReader ();
		//SqliteDataReader取表中数据的逻辑
		//首先,默认有一个指针指向表头,有一个方法让这个指针向下移动一行
		//然后通过列数拿到对应的值,然后指针再往下移动
		//Read() 读取一行
		while(reader.Read()){
			//取出uid
			int uid = reader.GetInt32(0);
			//取出name
			string name = reader.GetString(1);
			//通过字典方式拿去name
			//name = reader["name"].ToString();
			//取出score
			string score = reader.GetInt32(2);
		}
		command.Dispose ();
		reader.Close ();
	}
	

	void Destroy(){
		con.Close();//关闭数据库
	}
}

  

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

public class SqlBase :SingleTon<SqlBase>{
   
    
    #region 数据库创建对象类
    //数据库指令类
    SqliteCommand command;
    //数据库连接类
    SqliteConnection con;
    //数据库读取类
    SqliteDataReader reader;
    public SqlBase(){ }
    #endregion
    #region 打开数据库

    public void OpenSql(string dataName)
    {
        string sqlName = "";
        if (!dataName.Contains(".sqlite"))
        {
            sqlName = dataName + ".sqlite";
        }
        string dataStr = "Data Source =  " + Application.streamingAssetsPath + "/" + sqlName;
        con = new SqliteConnection(dataStr);
        command = con.CreateCommand();
        con.Open();
    }
    #endregion
    #region 关闭数据库

    public void CloseSql()
    {
        try
        {
            if (reader != null)
            {
                con.Close();
            }
            con.Close();
        }
        catch (SqliteException ex)
        {
            Debug.Log(ex.ToString());
        }
    }
    #endregion
    #region 查询单个数据

    public object SelectSingleData(string sqlStr)
    {
        try
        {
            command.CommandText = sqlStr;
            object obj = command.ExecuteScalar();
           
            return obj;
    }
        catch (SqliteException ex)
        {
            Debug.Log(ex.ToString());
            return null;
        }
    }
    #endregion
    #region 查询多个数据

    public List<ArrayList> SelectDatas(string sqlStr)
    {
        try {
            List<ArrayList> datas = new List<ArrayList>();
            command.CommandText = sqlStr;
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                ArrayList temp = new ArrayList();
                for(int i = 0; i < reader.FieldCount; i++)
                {
                    temp.Add(reader.GetValue(i));
                }
                datas.Add(temp);
            }
            reader.Close();
            return datas;
        }
        catch(SqliteException ex)
        {
            Debug.Log(ex.ToString());
            return null;
        }
        
    }
    #endregion
    #region 数据的增删改

    public void RunSql(string sqlStr)
    {
        try {
            command.CommandText = sqlStr;
            command.ExecuteNonQuery();
        }catch(SqliteException ex)
        {
            Debug.Log(ex.ToString());
        }

    }
    #endregion
}

  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值