linux@linux:~/test/sqlite$ sqlite3
SQLite version 3.7.2
Enter ".help"for instructions
Enter SQL statements terminated with a ";"
sqlite>
sqlite>.quite//退出
sqlite>insert into values (value1, value2,…);
sqlite> insert into stu values(1001,'zhangsan',88);
sqlite> insert into stu values(1002,"lisi",88);
sqlite> insert into stu values(1002,"王五",88);
sqlite> insert into stu values(1002,'赵六',88);
sqlite>
添加一条记录的部分字段
sqlite> insert into stu (name,score)values('王五',96);
sqlite> select *from stu;1001|zhangsan|761002|lisi|86|王五|96
sqlite>
7.2.3,查询表中记录
sqlite>select * from ; //查询所有字段
sqlite> select * from stu;1001|zhangsan|881002|lisi|881002|王五|881002|赵六|88
sqlite>
select name,score from ; //查询指定的字段
sqlite> select name from stu;
zhangsan
lisi
王五
sqlite>
sqlite>select * from where < expression> ; //按条件查询数据库中的内容
select * from where score = 80;
select * from where score = 80 and name= ‘zhangsan’;
select * from where score = 80 or name=‘wangwu’;
select * from where score >= 85 and score < 90;
sqlite> select * from stu where score=76;1001|zhangsan|76
sqlite>
7.2.4,按指定条件删除表中记录
sqlite> delete from where id=1003 and name=‘zhangsan’;
不加where会删除整张表
sqlite> select *from stu;1001|zhangsan|761002|lisi|86|王五|96
sqlite> delete from stu where id=1002;
sqlite> select *from stu;1001|zhangsan|76|王五|96
sqlite>
7.2.5,更新表中记录(修改)
sqlite>update set , … where < expression>;
sqlite> select *from stu;1001|zhangsan|76|王五|96
sqlite> update stu set id=1002 where name='王五';
sqlite> select * from stu;1001|zhangsan|761002|王五|96
sqlite>
7.2.6, 在表中添加字段
sqlite>alter table add column default …;
sqlite> alter table stu add column address char;
sqlite> select * from stu;1001|zhangsan|76|1002|王五|96|
sqlite>.schema
CREATE TABLE stu(id integer,name char,score integer, address char);
sqlite> update stu set address="beijing";
sqlite> select * from stu;1001|zhangsan|76|beijing
1002|王五|96|beijing
sqlite>
7.2.7,在表中删除字段
Sqlite3中不允许删除字段,可以通过下面步骤达到同样的效果
步骤
操作
创建一张新表,并从原表中提取字段
sqlite> create table stu as select id, name, score from student
删除原表
sqlite> drop table student
将新表名字改为原表
sqlite> alter table stu rename to student
sqlite> select * from stu;1001|zhangsan|76|beijing
1002|王五|96|beijing
sqlite> create table stu1 as select id,name,score from stu;
sqlite>.table
stu stu1
sqlite> drop table stu;
sqlite>.table
stu1
sqlite> alter table stu1 rename to stu;
sqlite>.table
stu
sqlite> select * from stu;1001|zhangsan|761002|王五|96
sqlite>
7.2.8,删除表
sqlite>drop table
7.3,Sqlite中判断表是否存在的方法
sqlite会自动维护一个系统表sqlite_master,该表存储了我们所创建的各个table, view, trigger等等信息。
sqlite_master表数据字段:
type: 类型,取值一般为table, view
name:
tbl_name: 表名
rootpage:
sql:创建表或者视图的sql语句,可以从该sql语句中判断某字段是否存在
sqlite_master表结构如下:
CREATE TABLE sqlite_master (
type TEXT,
name TEXT,
tbl_name TEXT,
rootpage INTEGER,
sql TEXT
);
7.3.1,查看这个内建表的所有记录:
select * from sqlite_master
7.3.2,查询sqlite中所有表
select name from sqlite_master where type=‘table’ order by name;
7.3.3,查询sqlite中指定表
select * from sqlite_master where type = ‘table’ and name = ‘t_cmpt_cp’
8,SQLite编程接口
8.1,打开(或创建)sqlite数据库 sqlite3_open()
int sqlite3_open(char *path, sqlite3 **db);
path:数据库文件路径
db:指向sqlite句柄的指针(数据库的操作句柄)
返回值:成功返回0(SQLITE_OK),失败返回错误码(非零值)。If the database is opened (and/or created) successfully, then SQLITE_OK is returned. Otherwise an error code is returned.
intsqlite3_open(constchar*filename,/* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */);intsqlite3_open16(constvoid*filename,/* Database filename (UTF-16) */
sqlite3 **ppDb /* OUT: SQLite db handle */);intsqlite3_open_v2(constchar*filename,/* Database filename (UTF-8) */
sqlite3 **ppDb,/* OUT: SQLite db handle */int flags,/* Flags */constchar*zVfs /* Name of VFS module to use */);
intsqlite3_exec(
sqlite3*,/* An open database */constchar*sql,/* SQL to be evaluated */int(*callback)(void*,int,char**,char**),/* Callback function */void*,/* 1st argument to callback *///给回调函数传参
char**errmsg /* Error msg written here */);
8.4.1 找到一条记录自动执行一次回调函数
typedef int (*sqlite3_callback)(void *para, int f_num, char **f_value, char **f_name);
para:传递给回调函数的参数
f_num:记录中包含的字段数目
f_value:包含每个字段值的指针数组
f_name:包含每个字段名称的指针数组
返回值:成功返回0,失败返回-1
8.5,不使用回调函数执行SQL语句(操作)
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
db:数据库句柄
sql:SQL语句
resultp:用来指向sql执行结果的指针
nrow:满足条件的记录的数目
ncolumn:每条记录包含的字段数目
errmsg:错误信息指针的地址
返回值:成功返回0,失败返回错误码
9,示例
#include#include#include#include#define DATABASE "stu.db"/* 如果定义了CALLBACK,查询时,就用回调函数 *///#define CALLBACK voidoperate_table_cmd_menu(){puts("/******************************************/");puts("*operate table cmd menu:");printf("*1:insert 2:delete 3:query 4:update 5:quite
");puts("/******************************************/");printf("Please input cmd:");}voidoperate_table_type_menu(char*str){puts("/******************************************/");printf("*%s table by:
",str);if(strncasecmp(str,"update",strlen("update"))==0)printf("*1:id 2:name 3:score 4:don't %s
",str);elseprintf("*1:id 2:name 3:score 4:all 5:don't %s
",str);puts("/******************************************/");printf("Please input type:");}intdo_insert_sqlite3(sqlite3 * db);intdo_delete_sqlite3(sqlite3 *db);intdo_query_sqlite3(sqlite3 * db);intquery_callback(void*para,int f_num,char**f_value,char**f_name);intdo_update_sqlite3(sqlite3 * db);constchar*get_update_handle(char* set_str);intmain(int argc,constchar*argv[]){
sqlite3 * db;char*errmsg;int cmd;//打开sqlite3数据库if(sqlite3_open(DATABASE,&db)!= SQLITE_OK){printf("%s
",sqlite3_errmsg(db));exit(-1);}else{printf("open %s success.
",DATABASE);}//创建一张数据库的表格 将ID设为主键值,并设为自增字段(保证字段唯一性)if(sqlite3_exec(db,"create table stu(id INTEGER PRIMARY KEY AUTOINCREMENT,name char,score Integer);",NULL,NULL,&errmsg)!= SQLITE_OK){printf("%s
",errmsg);}else{printf("create or open table success.
");}while(1){operate_table_cmd_menu();while(scanf("%d",&cmd)==0){getchar();operate_table_cmd_menu();}getchar();switch(cmd){case1:do_insert_sqlite3(db);break;case2:do_delete_sqlite3(db);break;case3:do_query_sqlite3(db);break;case4:do_update_sqlite3(db);break;case5:sqlite3_close(db);exit(0);default:printf("Error cmd.
");}}return0;}intdo_insert_sqlite3(sqlite3 * db){// int id;char name[32]={};int score;char sql[128];char*errmsg;int newline_ok =0;/*
printf("input student id:");
scanf("%d",&id);
getchar();
*/printf("input student name:");scanf("%s",name);getchar();printf("input student score:");scanf("%d",&score);getchar();sprintf(sql,"insert into stu values(NULL,'%s',%d);",name,score);//自增字段的值传NULLif(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!= SQLITE_OK ){printf("%s
",errmsg);return-1;}else{printf("insert: ");sprintf(sql,"select * from stu where id=%d;",(int)sqlite3_last_insert_rowid(db));sqlite3_exec(db,sql,query_callback,&newline_ok,&errmsg);printf("done
");return0;}}intdo_delete_sqlite3(sqlite3 * db){int id;char name[32]={};int score;char sql[128];char*errmsg;int type;int return_val=0;int newline_ok;char y_Y[5];operate_table_type_menu("delete");while(scanf("%d",&type)==0){getchar();operate_table_type_menu("delete");}getchar();switch(type){case1:{printf("input student id:");scanf("%d",&id);getchar();
newline_ok =1;printf("Sure you want to delete:
");sprintf(sql,"select * from stu where id=%d;",id);sqlite3_exec(db,sql,query_callback,&newline_ok,&errmsg);printf("if you sure,Please input y|Y: ");scanf("%s",y_Y);if(strncasecmp(y_Y,"y",strlen("y"))==0){sprintf(sql,"delete from stu where id=%d;",id);if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!= SQLITE_OK ){printf("%s
",errmsg);
return_val =-1;}else{printf("delete done.
");
return_val =0;}}else{
return_val =0;goto _cancel;}break;}case2:{printf("input student name:");scanf("%s",name);getchar();
newline_ok =1;printf("Sure you want to delete:
");sprintf(sql,"select * from stu where name='%s';",name);sqlite3_exec(db,sql,query_callback,&newline_ok,&errmsg);printf("if you sure,Please input y|Y: ");scanf("%s",y_Y);if(strncasecmp(y_Y,"y",strlen("y"))==0){sprintf(sql,"delete from stu where name='%s';",name);if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!= SQLITE_OK ){printf("%s
",errmsg