Sqlite学习

一,Sqli简介

SQLite是遵守ACID的关联式数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。不像常见的客户-服务器范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分。所以主要的通信协议是在编程语言内的直接API调用。这在消耗总量、延迟时间和整体简单性上有积极的作用。整个数据库(定义、表、索引和数据本身)都在宿主主机上存储在一个单一的文件中。它的简单的设计是通过在开始一个事务的时候锁定整个数据文件而完成的。

二,环境配置

Sqlite官网地址:http://www.sqlite.org/

Sqlite3文件下载:

1.下载 sqlite-dll-win32-x86-3140100

2.下载 sqlite-tools-win32-x86-3140100

安装:

先在C盘建一个文件夹 sqlite,

1.把 sqlite-dll-win32-x86-3140100 中解压出来的两个文件(sqlite3.def 和 sqlite3.dll)复制到刚才新建的目录(C:\sqlite)中

2.把 sqlite-tools-win32-x86-3140100 中解压出来的文件 sqlite3.exe 复制到C:\sqlite 目录中

3.添加系统环境变量, 在Path变量值后面添加 C:\sqlite(分号不要忘记了),如下图所示:

接下来测试一下是否安装完成

运行cmd,切换到C盘根目录,输入sqlite3,然后回车,如果安装成功的话,会显示sqlite版本号,如下图所示:

OK,Sqlite3已经安装完成了。

三,常用指令

//转到桌面
cd C;\User\hasee\Desktop 
//打开数据库文件,如果没有创建
sqlite3 data.db
//列出表名
.table
//创建表
create table UESR(uid integer,name text,score text);
//删除表格
drop table USER;
//主键自增
create table if not exists USER(uid integer primary key autoincrement,name text,score text);
插入信息
insert into USER values(1,'郭靖',89);
//插入信息
insert into USER(name,score) vlaues('杨过','89');
//修改
update USER set name = '黄老邪'where name='李寻欢';
//展示表中的所有信息
select*from USER;
//以UID删除
delete from USER where uid = 4;
//返回有几行
select count(*) from USER;
//返回总和
select sum(score)from USER;
select*from USER where score >90 and score<95;
select *from USER limit 2;//前两条
select *from USER order by score;//正序排
select *from USER order by score desc;//倒序排
select *from USER order by score desc limit 1;//倒数第一个
.exit
select USER.name,USER.score,KUNGFU.name from USER,KUNGFU where USER.uid  = KUNGFU.uid;
unique 唯一键  修饰字段的

 

四,unity中常用类

  在unity中一般将Sqlite的一系列操作写在一个类中,并设置为单例模式,然后其他指令操作封装为函数

  1.SqliteConnection类:

  建立数据库链接,需要提供链接地址,如果不存在该数据库,则会新建一个你提供文件名的数据库。 

  

  public static SqliteManager Instance;
1
void Awake () { 2 Instance = this; 3 con = new SqliteConnection("Data Source="+Application.dataPath+"/Data/data.db"); 4 con.Open(); 5 }

 

  2.SqliteCommand类

   执行数据库执行命令

1  public void executeNonQuery(string sqlStr) {
2         command = new SqliteCommand(sqlStr,con);
3         command.ExecuteNonQuery();
4         command.Dispose();
5         return;
6     }

  封装后可以在直接调用,例如下面建表

 

1 public string str="create table if not exists USER(uid integer primary key autoincrement,name text,password text)";
2 SqliteManager.Instance.executeNonQuery(str); 

 

  3.SqliteDataReader类

1   //执行数据库查询封装
2     public SqliteDataReader  executeQuery(string sqlStr) {
3         command = new SqliteCommand(sqlStr,con);
4         SqliteDataReader reader = command.ExecuteReader();
5         command.Dispose();
6         return reader;
7     }

  封装后调用

 1 SqliteDataReader reader =SqliteManager.Instance.executeQuery ("select * from ITEM where uid=" + uid);//* 

 

转载于:https://www.cnblogs.com/xiaoyaogegelxd/p/6707751.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值