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. [转]access 标准表达式中数据类型不匹配

    好久没有用access,今儿遇到一个特别让人无语的问题: access数据表的Date/Time类型的字段,假如字段名为dtime: 如果直接用dtime=‘2013/9/6 10:50:21’,sq ...

  2. gentoo openrc 开机打印信息

    gentoo openrc 开机的时候,最开始 一些硬件的信息, 后面是一些内核和驱动的信息. 硬件的信息是默认保存到 /var/log/dmesg 中, 可以使用 dmesg | less 来查看硬 ...

  3. spring 之 lookup-method & replace-method

    初始化bean的堆栈: at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy$Cglib ...

  4. GPUImage中曝光滤镜实现——GPUImageExposureFilter

    核心代码: varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform highp float ...

  5. hadoop动态添加删除节点datanode及恢复

    1. 配置系统环境 主机名,ssh互信,环境变量等 本文略去jdk安装,请将datanode的jdk安装路径与/etc/hadoop/hadoop-evn.sh中的java_home保持一致,版本ha ...

  6. tensorflow实战系列(一)

    最近开始整理一下tensorflow,准备出一个tensorflow实战系列,以飨读者. 学习一个深度学习框架,一般遵循这样的思路:数据如何读取,如如何从图片和标签数据中读出成tensorflow可以 ...

  7. 机器学习进阶-直方图与傅里叶变换-傅里叶变换(高低通滤波) 1.cv2.dft(进行傅里叶变化) 2.np.fft.fftshift(将低频移动到图像的中心) 3.cv2.magnitude(计算矩阵的加和平方根) 4.np.fft.ifftshift(将低频和高频移动到原来位置) 5.cv2.idft(傅里叶逆变换)

    1. cv2.dft(img, cv2.DFT_COMPLEX_OUTPUT) 进行傅里叶变化 参数说明: img表示输入的图片, cv2.DFT_COMPLEX_OUTPUT表示进行傅里叶变化的方法 ...

  8. xampp默认mysql数据库root密码的修改

    因为安装xampp后的mysql默认用户root的密码为空,而比如部署Testlink时需要提供数据库密码,此时就需要给root设定密码(网上有些方法,大同小异,但是可能都未标明关键点,未一些出上手的 ...

  9. newCachedThreadPool使用案例

    newCachedThreadPool 缓存默认60s 猜下你的结果 package com.juc.threadpool; import java.util.concurrent.ExecutorS ...

  10. Vue router 的使用--初级

    在说 VueRouter 之前,首先要弄明白vueRouter 是干什么的,有什么用 说出来其实很简单,就是一个模板替换的问题,当路由改变的时候,把和路由相关的模板显示出来,就是这么简单.但是,当我们 ...