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. js 数组对象的操作方法

    在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...

  2. RPM软件管理工具

    1 概述 RPM(RedHat Package Manager),Rethat软件包管理工具,类似windows里面的setup.exe 是Linux这系列操作系统里面的打包安装工具,它虽然是RedH ...

  3. Xcode6在iPhone5+iOS7模拟器上编译,上下有黑边问题

    http://94it.net/a/jingxuanboke/2015/0113/447679.html

  4. 16.Mongodb安装

    Mongodb是由c++编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档.数组及文档数组,非常灵活. 1.相关链接: http ...

  5. 《算法》第六章部分程序 part 2

    ▶ 书中第六章部分程序,包括在加上自己补充的代码,B-树 ● B-树 package package01; import edu.princeton.cs.algs4.StdOut; public c ...

  6. PC浏览器播放m3u8

    HLS(HTTP Live Streaming)是苹果公司针对iPhone.iPod.iTouch和iPad等移动设备而开发的基于HTTP协议的流媒体解决方案.在 HLS 技术中 Web 服务器向客户 ...

  7. [转] golang socket

    server.go package main import ( "net" "fmt" "io/ioutil" "time&quo ...

  8. 上下两个div, 一个固定高度, 另一个铺满屏幕

    <div class="box"> <div class="el1"></div> <div class=" ...

  9. 根据svm将视频帧转换为img

    # -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: Manuel " ...

  10. day21-类的组合

    一.面向对象的组合用法 软件重用的重要方式除了继承之外还有另外一种方式,即:组合组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合 用组合的方式建立了类与组合的类之间的关系,它是一种‘ ...