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. 20165205 2017-2018-2 《Java程序设计》 第二周学习总结

    20165205 2017-2018-2 <Java程序设计>第一周学习总结 教材学习内容总结 学习第二章知识(标识符,关键字,数据类型,输入.输出数据和数组) 学习第三章知识(运算符,基 ...

  2. windows下mvn verify异常

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jarsigner-plugin:1.4:sign (sign) on pr ...

  3. Dubbo(1)--初识Dubbo

    1. 为什么需要 Dubbo 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当 ...

  4. mybatis二(参数处理和map封装及自定义resultMap)

    .单个参数 mybatis不会做特殊处理. #{参数名/任意名}:取出参数值. .多个参数 mybatis会做特殊处理. 多个参数会被封装成 一个map. key:param1...paramN,或者 ...

  5. kvm虚拟机相关

    一.虚拟机与宿主机鼠标不同步问题: https://blog.csdn.net/u012255731/article/details/53006195 先关闭虚拟机,想要修改鼠标和宿主机界面同步方法如 ...

  6. hive数据倾斜原因以及解决办法

    何谓数据倾斜?数据倾斜指的是,并行处理的数据集 中,某一部分(如Spark的一个Partition)的数据显著多于其它部分,从而使得该部分的处理速度成为整个数据集处理的瓶颈. 表现为整体任务基本完成, ...

  7. PostMessage实现多窗口之间的数据传递

    [本文参考自网络,参考地址:https://blog.csdn.net/lsyyoyo/article/details/38726419] 1.在公共的头文件中添加一个宏定义: #define WM_ ...

  8. API / DOM

    浏览器特性 当控制台报错时,IE9会停止执行JS.当打开控制台时会执行后续JS ------------------------------------------------------------ ...

  9. shell中的逻辑判断while

    w|head -1|awk -F'load average: ' '{print $2}'|cut -d. -f1 #!/bin/bash while true do load=`w|head -1| ...

  10. java json字符串与对象转换

    下载引入包gson-2.0.jar 1.字符转数据 final Map map = new HashMap();map.put("contents",    "[{\&q ...