SQLite的源代码是C,其源码完全开放,SQLite第一个Alpha版本诞生于2000年。他是一个轻量级的嵌入式数据库。
SQLite有以下特性:
零配置(无需安装和管理配置)
储存在单一磁盘文件中的一个完整的数据库;
数据库文件可以在不同字节顺序的机器间自由共享
支持数据库大小至2TB
足够小,全部源码大致3万行c代码,250kb;
比目前流行的大多数数据库对数据的操作要快
1.创建一张表
Create table stuinfo(id integer,name text,age integer,socre float);
- 插入一条记录
Insert into stuinfo values(1001,’zhangsan’,18,80);
Insert into stuinfo(id,name,score)values(1002,’lisi’,90);
- 查看数据库记录
Select * from stuinfo
Select * from stuinfo where score = 80;
Select * from stuinfo where socre=80 and name=’zhangsan’;
Select * from stuinfo where socre=80 or name=’wangwu’;
Select name,score from stuinfo;查询指定的字段
Select * from stuinfo where score >=85 and score <90;
- 删除一条记录
Delete from stuinfo where id = 1003 and name=’zhangsan’;
- 更新一条记录
Update stuinfo set age=20 where id=1003;
Update stuinfo set age=30,score=82 where id=1003;
- 删除一张表
Drop table stuinfo
- 增加一列
Alter table stuinfo add column sex char;
- 删除一列
Create table stu as select id,name,score from stuinfo;
Drop table stuinfo;
Alter table stu rename to stuinfo;
数据库设置主键:
Create table info(id integer primary key autoincrement,name vchar);
Sqlite3 数据库 c语言API
Int sqlite3_open(
Const char *filename,
Sqlite3 **ppDb
);
功能:打开数据库
参数:filename 数据库名称
ppdb 数据库句柄
返回值:成功为0 SQLITE_OK,出错 错误码
Int sqlite3_close(sqlite3* db);
功能:关闭数据库;
参数:
返回值:成功为0 SQLITE_OK,出错 错误码
Const char *sqlite3_errmsg(sqlite*db);
功能:得到错误信息的描述
Int sqlite3_exec(
Sqlite3* db,
Const char *sql,
Int (*callback)(void* arg,int,char**,char**),
Void *arg,
Char **errmsg
);
功能:执行一条sql语句
参数:db 数据库句柄
Sql sql语句
Callback 回调函数,只有在查询时,才传参
arg 为回调函数传递参数
Errmsg 错误消息
返回值:成功SQLITE_OK
查询回调函数:
Int (*callback)(void* arg,int ncolumns,char** f_value,char** f_name),
功能:查询语句执行之后,会回调此函数
参数:arg 接收sqlite3_exec传递来的参数
Ncolumns 列数
f_value 列的值的地址
F_name 列的名称
返回值: 0
Int sqlite3_get_table(
Sqlite3 *db,
Const char *zSql,
Char ***pazResult,
Int *pnRow,
Int *pnColumn,
Char **pzErrmsg
);
Void sqlite3_free_table(char **result);