一、基础用法:
当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端,以sqlite>前缀标识:
#sqlite3 test.db
查看数据库文件信息命令(注意命令前带字符'.'):
sqlite>.database
查看所有表的创建语句:
sqlite>.schema
查看指定表的创建语句:
sqlite>.schema table_name
以sql语句的形式列出表内容:
sqlite>.dump table_name
设置显示信息的分隔符:
sqlite>.separator symble
Example:设置显示信息以‘:’分隔
sqlite>.separator :
设置显示模式:
sqlite>.mode mode_name
Example:默认为list,设置为column,其他模式可通过.help查看mode相关内容
sqlite>.mode column
输出帮助信息:
sqlite>.help
设置每一列的显示宽度:
sqlite>.width width_value
Example:设置宽度为2
sqlite>.width 2
列出当前显示格式的配置:
sqlite>.show
退出sqlite终端命令:
sqlite>.quit
或
sqlite>.exit
二、基操:
filed ---> 表示表中每一项名字
1、建表
create table 表名 if not exists(field1 type1, field2 type2, ...);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
例,建立一个简单的学生信息表,它包含学号与姓名等学生信息:
create table student_info(stu_no interger primary key, name text);
2、添加数据
insert into 表名(field1, field2, ...) values(val1, val2, ...);
valx为需要存入字段的值。
例,往学生信息表添加数据:
Insert into student_info(stu_no, name) values(0001, 'alex');
Insert into student_info(stu_no, name) values(0003, 'lc');
3、修改数据记录
update 表名 set field1=val1, field2=val2 where expression;
where是sql语句中用于条件判断的命令,expression为判断表达式
例,修改学生信息表学号为0001的数据记录:
update student_info set stu_no=0001, name='hence' where stu_no=0001;
4、删除数据记录
delete from 表名 where expression;
不加判断条件则清空表所有数据记录。
例,删除学生信息表学号为0001的数据记录:
delete from student_info where stu_no=0001;
5、查询数据记录
select指令基本格式:
select columns from 表名 [where expression];
a 查询输出所有数据记录
select * from 表名;
b 限制输出数据记录数量 val =3 查询前三行
select * from 表名 limit val;
c 升序输出数据记录
select * from 表名 order by field asc;
d 降序输出数据记录
select * from 表名 order by field desc;
e 条件查询
select * from 表名 where expression;
select * from 表名 where field in ('val1', 'val2', 'val3');
select * from 表名 where field between val1 and val2;
f 查询记录数目
select count (*) from 表名;
g 区分列数据
select distinct field from 表名;
有一些字段的值可能会重复出现,distinct去掉重复项,将列中各字段值单个列出。
6、增加一个字段addr
alter table 表名 add column addr type;
例如在food表中添加一个count列
alter table food add column count int;
7、改变表名
alter table 旧表名 rename to 新表名;
8、建立索引
当说数据表存在大量记录,索引有助于加快查找数据表速度。
create index index_name on 表名(field);
例,针对学生表stu_no字段,建立一个索引:
create index student_index on student_table(stu_no);
建立完成后,sqlite3在对该字段查询时,会自动使用该索引。
9、删除数据表或者索引
drop table 表名;
drop index 索引名;
10、时间类型插入数据库表中
DATETIME TIMESTAMP 用来存储日期/时间的不限制长度的字符串类型.
要求的格式是 ‘YYYY-MM-DD HH:MM:SS’,其他的东西被忽略.
create table tablename (id int,time DATETIME);
insert into tablename values(1003,datetime('now')); 插入1003和当前系统时间到表中
select * from tablename where time between '2018-01-10' and '2018-01-19'; 查询表中在2018-01-10到208-01-19号之间所有数据
select id from tablename where time > '2018-01-10 15:29:06'; 查询tablename表中时间大于2018-01-10 15:29:06的所有id项数据
三、sqlite3 函数使用
1、打开数据库
打开数据库链接sqlite3_open用法
原型:
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
参数一:
数据库文件名字,不一定存在,不存在会自动创建
参数二:
第二个参数是一个 sqlite3 ** 类型的指针,用来接收打开的数据库连接对象。如果打开或创建成功,该指针会被设置为指向该连接对象的指针;如果打开或创建失败,该指针会被设置为空值(NULL)。
返回值:
如果是SQLITE_OK则表示操作正常
2、关闭数据库
关闭数据库链接sqlite3_close用法
原型:
int sqlite3_close(sqlite3 *ppDb);
ppDb为刚才使用sqlite3_open打开的数据库链接
3、sql操作sqlite3_exec用法
执行sql操作sqlite3_exec用法
原型:
int sqlite3_exec(
sqlite3* ppDb, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
这就是执行一条sql 语句的函数。
参数:
第1个参数是前面open函数得到的指针。
第2个参数constchar*sql是一条sql 语句,以\0结尾。
第3个参数sqlite3_callback 是回调,当这条语句执行之后,sqlite3会去调用你提供的这个函数。
第4个参数void*是你所提供的指针,你可以传递任何一个指针参数到这里,这个参数最终会传到回调函数里面,如果不需要传递指针给回调函数,可以填NULL。等下我们再看回调函数的写法,以及这个参数的使用。
第5个参数char** errmsg 是错误信息。注意是指针的指针。sqlite3里面有很多固定的错误信息。执行sqlite3_exec 之后,执行失败时可以查阅这个指针(直接cout<<errmsg得到一串字符串信息,这串信息告诉你错在什么地方。sqlite3_exec函数通过修改你传入的指针的指针,把你提供的指针指向错误提示信息,这样sqlite3_exec函数外面就可以通过这个char*得到具体错误提示。
PS:通常,sqlite3_callback 和它后面的void*这两个位置都可以填NULL。填NULL表示你不需要回调。比如你做insert 操作,做delete操作,就没有必要使用回调。而当你做select 时,就要使用回调,因为sqlite3 把数据查出来,得通过回调告诉你查出了什么数据。
4、exec 的回调
typedef int(*sqlite3_callback)(void*,int,char**,char**);
你的回调函数必须定义成上面这个函数的类型。下面给个简单的例子:
//sqlite3的回调函数
//sqlite 每查到一条记录,就调用一次这个回调
int LoadMyInfo(void* para,intn_column,char** column_value,char** column_name);
//para是你在sqlite3_exec 里传入的void*参数通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针),
//然后在这里面强制转换成对应的类型(这里面是void*类型,必须强制转换成你的类型才可用)。然后操作这些数据
//n_column是这一条记录有多少个字段(即这条记录有多少列)
//char** column_value 是个关键值,查出来的数据都保存在这里,它实际上是个1维数组(不要以为是2维数组),
//每一个元素都是一个char*值,是一个字段内容(用字符串来表示,以\0结尾)
//char** column_name 跟column_value是对应的,表示这个字段的字段名称
5、示例
C++测试:
#include <iostream>
using namespace std;
#include "sqlite/sqlite3.h"
int callback(void*,int,char**,char**);//回调函数申明
int main()
{
// ppDb:句柄(指针),用来表示数据库的地址
sqlite3* db;
//成功则返回SQLITE_OK的宏
int nResult = sqlite3_open("test.db",&db);
if (nResult != SQLITE_OK)
{
cout<<"打开数据库失败:"<<sqlite3_errmsg(db)<<endl;
return 0;
}
else
{
cout<<"数据库打开成功"<<endl;
}
//错误信息的存放
char* errmsg;
//exec函数族的调用。
nResult = sqlite3_exec(db,"create table fxck(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg;
sqlite3_free(errmsg);
return 0;
}
string strSql;
strSql+="begin;\n";
for (int i=0;i<100;i++)
{
strSql+="insert into fuck values(null,'heh');\n";
}
strSql+="commit;";
//cout<<strSql<<endl;
nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
//查看表中所有数据
strSql = "select * from fuck";
//回调函数
nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);
if (nResult != SQLITE_OK)
{
sqlite3_close(db);
cout<<errmsg<<endl;
sqlite3_free(errmsg);
return 0;
}
sqlite3_close(db);
return 0;
}
int callback(void* ,int nCount,char** pValue,char** pName)
{
string s;
for(int i=0;i<nCount;i++)
{
s+=pName[i];
s+=":";
s+=pValue[i];
s+="\n";
}
cout<<s<<endl;
return 0;
}
C语言测试:
//exec族函数
char sql[100];
snprintf(sql, sizeof(sql), "SELECT *FROM group_usr where name != '%s'",request->username);
// data_t request_send = request;
//exec第三个参数表示回调函数地址,第四个为回调函数第一参数,
int req = sqlite3_exec(db,sql,callback_func_grop,request,NULL);
if (req != SQLITE_OK) {
fprintf(stderr, "无法执行发送操作: %s\n", sqlite3_errmsg(db));
}
//回调函数
int value;
int callback_func_grop(void* arg,int column,char**fd, char** name){
sleep(1);
int i=0;
printf("回调函数发送信息1\n");
char *p;
//定义接收结构体,打包发送信息
data_t* abg = (data_t*)arg;
data_t other;
other.work = 4;
//strcpy(other.othername,abg->othername);
strcpy(other.msg,abg->msg);
//printf("列的数目有%d个\n",column);
printf("回调函数发送信息2\n");
//for(i=0;i<column;i++){}
//int* callback_result =(int*)malloc(10);
// *callback_result = atoi(fd[i]);
value = atoi(fd[i]);
strcpy(p,pack_user(other));
printf("%d",value);
int jg=send(value,p, strlen(p), 0);
if(jg < 0||jg ==0){
printf("发送失败\n");
}
bzero(p,strlen(p));
return 0;
}