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
}

  

Unity&Sqlite数据库的更多相关文章

  1. Unity sqlite学习笔记一

    1.SQLITE的常识 SQLite是一个开源免费的数据库,一般用于嵌入系统或者小规模的应用软件开发中,你可以像使用Access一样使用它. sqlite的主要优点:零配置(Zero Configur ...

  2. unity3d sqlite数据库的读写方法

    首先,我们要从unity的安装路径中复制mono.data.sqlite.dll和sqlite3.dll两个动态链接库到untiy的plugins目录下,如下图所示: 使用navicat for sq ...

  3. 14 SQLite数据库

    SQLite数据库SQLite 是一款轻型的数据库SQLite 的设计目标是嵌入式的SQLite 占用资源低SQL 指结构化查询语言SQL 使我们有能力访问数据库SQL 是一种 ANSI 的标准计算机 ...

  4. Android之SQLite数据库篇

    一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...

  5. Qt5 开发 iOS 应用之访问 SQLite 数据库

    开发环境: macOS 10.12.1 Xcode 8.1 Qt 5.8 iPhone 6S+iOS 10.1.1   源代码: 我在 Qt 程序里指定了数据库的名称来创建数据库,在 Win10.An ...

  6. 【Win 10 应用开发】Sqlite 数据库的简单用法

    如果老周没记错的话,园子里曾经有朋友写过如何在 UWP 项目中使用 Sqlite数据库的文章.目前我们都是使用第三方封装的库,将来,SDK会加入对 Sqlite 的支持. 尽管目前 UWP-RT 库中 ...

  7. Android之SQLite数据库使用

    转载整理于:http://my.csdn.net/lmj623565791 我刚开始接触Android的时候甚至都不敢相信,Android系统竟然是内置了数据库的!好吧,是我太孤陋寡闻了.由于我之前是 ...

  8. 让PDF.NET支持最新的SQLite数据库

    最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...

  9. iOS sqlite数据库图像化查看

    问题描述:在xocde上用sqlite数据库的时候,因为没有图形化界面,有些时候很难看出自己设计的数据库是否有问题,比如我刚上手sqlite数据库设计id为自增长时,很自然的用了identify(1, ...

随机推荐

  1. python协程的使用

    1.Python对协程的支持是通过generator(生成器 yield)实现的.协程执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行. 2.调用生成器运行的过程中 ...

  2. 【python】python与正则 re的主要用到的方法列举

    [直接上代码] #coding=utf-8#1.先将正则表达式的字符串形式编译为Pattern实例 #2.使用Pattern实例处理文本并获得匹配结果 #3.最后使用Match实例获得消息,进行其他操 ...

  3. Ruby学习笔记7: 添加身份验证(adding Authentication)

    我们已经完成了Category & Product页面内容的增删改查,再加入一个身份验证即可成为一个较完整的Rails App了.本文就来完成这个任务. We now need to give ...

  4. 8.Appium的基本使用-1

    1.Appium是什么?Appium是一个跨平台移动端自动化测试工具,可以便捷的为ios和Android平台创建自动化测试用例.模拟app内部各种操作,点击.滑动.文本输入等,手工操作的动作appiu ...

  5. <转载> 22种代码味道(Martin Fowler与Kent Beck) http://blog.csdn.net/lovelion/article/details/9301691

    Martin Fowler在Refactoring: Improving the Design of Existing Code(中译名:<重构——改善既有代码的设计>)一书中与Kent ...

  6. FLEX中一组基于button的组件

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  7. python linux 下开发环境搭建

    1.1: 在虚拟环境目录下安装 ipython  => pip install ipython 1.2: 简单的使用 => ipthyon => print("heollo ...

  8. vue格式化显示json数据

    已经是json格式数据的,直接用标签 <pre></pre>展示. 参考:https://www.jianshu.com/p/d98f58267e40

  9. centos服务器如何用命令查看哪个程序内存占用情况,硬盘空间占用

    1.free -m只能查看内存总量情况 2.top M ( 注意M是大写) 3.ps aux|head -1; ps aux | sort -k4nr | head -10 ------------- ...

  10. js记录用户访问页面和停留时间

    1.setInterval setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval(code,millisec[,"lang" ...