SQLite database
A database is a collection of data stored on storage media under the management and control of a database management system. Commonly used data is divided into large, medium and small databases.
1. SQLite database
SQLite is a lightweight embedded database, which takes up very low resources. In embedded devices, only a few hundred K of memory may be enough. Its processing speed is faster than the two famous databases Mysql and PostgreSQL. SQLite provides some C function interfaces, which can be used to manipulate the database. By using these interfaces, pass some standard SQL statements (in char * type) to the SQLite function, and SQLite will operate the database for you.
- Zero configuration, no need to install and manage configuration
- A complete database stored in a single disk file
- Database files can be shared freely between machines with different byte order
- Support database size up to 2TB
- Small enough, the entire source code is roughly 30,000 lines of C code, 250KB
- Operate data faster than most popular databases
2. Use of SQLite database
2.1 Database installation
本地安装:sudo dpkg -i *.deb
在线安装:sudo apt-get install sqlite3
2.2 Database system commands
启动SQLite3:sqlite
打开数据库文件:sqlite <*.db> (若文件不存在则自动创建该文件)
显示所有命令:.help
退出sqlite3:.quit
显示当前打开的数据库文件:.database
显示数据库中所有表名:.tables
查看表的结构:.schema <table name>
2.3 Database sql command
//sql命令一定要以“;”结束
//以下以表stdinfo为例
创建表: create table stuinfo(id integer, name text, age integer, score float);
插入记录:insert into stuinfo values(1, 'Andy', 30, 99.9);
insert into stuinfo(id, name, score) values(2, 'Amy', 100);
查看记录:select * from stuinfo;
select * from stuinfo where score = 100;
select * from stuinfo where score = 99.9 and name = 'Andy';
select * from stuinfo where score = 99.9 or name = 'Amy';
select * from stuinfo where score > 80 and score < 100;
select name,score from stuinfo; //查询指定的字段
删除记录:delete from stuinfo where id = 1 and name = 'Andy';
更新记录:update stuinfo set age = 20,score = 100 where id = 1;
增加一列:alter table stuinfo add column sex char;
删除一列:create table stu as select id, name, score from stuinfo; //从stuinfo中复制新表(不包含要删除的列)
drop table stuinfo; //删除老的表stuinfo
alter table stu rename to stuinfo; //将新表stu重命名为stuinfo,完成删除列的操作
删除表: drop table stuinfo;
/***设置主键***/
设置主键: create table table_name(column1 datatype PRIMARY KEY, column2 datatype, ……);
//PRIMARY KEY表示该列为该表的“主关键字”,主关键字用于唯一索引表内的某一条记录,主关键字必须唯一且主关键字的列值不能为空
//一张表允许省略主关键字
2.4 SQLite programming interface
- Open the database sqlite3_open()
函数原型:int sqlite3_open(char *path, sqlite3 **db)
输 入 值:path 数据库文件路径
db 指向sqlite句柄的指针
返 回 值:成功返回0;失败返回错误码(非零值)
- Close the database sqlite3_close()
函数原型:int sqlite3_close(sqlite3 *db)
输 入 值:db 指向sqlite句柄的指针
返 回 值:成功返回0;失败返回错误码(非零值)
- Return error message sqlite3_errmg()
函数原型:const char *sqlite3_errmg(sqlite3 *db)
输 入 值:db 指向sqlite句柄的指针
返 回 值:返回错误信息
- Perform SQL operations (using the callback function) sqlite3_exec()
函数原型:typedef int (*sqlite3_callback)(void*,int,char**,char**); //定义sqlite3_exec()需要的回调函数的函数指针类型
int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void* para, char **errmsg)
输 入 值:db 需要操作的数据库的句柄指针
sql 需要对数据库执行的SQL语句
callback 回调函数,当执行SQL语句(第二个参数)成功后会自动运行该函数;如果不需要则设为NULL
para 传递给回调函数第一个参数的指针;如果不需要则设为NULL
errmsg 存储错误信息指针
返 回 值:成功返回0;失败返回错误码(非零值)
- Callback
函数原型:int function(void *para, int f_num, char *f_value[], char *f_name[])
输 入 值:para 传递给回调函数的参数(即sqlite3_exec()的第四个参数)
f_num 记录中包含的字段数目(列数)
f_value 包含每个字段值的指针数组(列的值)
f_name 包含每个字段名称的指针数组(列的名称)
返 回 值:成功返回0;失败返回-1
- Perform SQL operations (without using the callback function) sqlite3_get_table()
函数原型:int sqlite3_get_table(
sqlite3 *db, /* 数据库句柄 */
const char *zSql, /* 需要对数据库执行的sql语句 */
char ***pazResult, /* 用来指向SQL执行结果的指针 */
int *pnRow, /* 满足条件的记录的数目 */
int *pnColumn, /* 每条记录包含的字段数目 */
char **pzErrmsg /* 错误信息指针的地址 */
)
返 回 值:成功返回0;失败返回错误码(非零值)