sqlite3数据库命令及函数接口.docx_第1页
sqlite3数据库命令及函数接口.docx_第2页
sqlite3数据库命令及函数接口.docx_第3页
sqlite3数据库命令及函数接口.docx_第4页
sqlite3数据库命令及函数接口.docx_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

sqlite常用命令及编程接口介绍作者:冯利美,华清远见嵌入式学院讲师。一、常用命令介绍在终端下运行sqlite3 ,出现如下提示符:SQLite version 3.7.2Enter “.help” for instructionsEnter SQL statements terminated with a “;”sqlite 是要打开的数据库文件。若该文件不存在,则自动创建。 显示所有命令 sqlite .help退出sqlite3sqlite.quit显示当前打开的数据库文件 sqlite.database显示数据库中所有表名 sqlite.tables查看表的结构 sqlite.schema /*/以下为SQL命令,每个命令以;结束 创建新表 create table (f1 type1, f2 type2,);sqlite create table student(no integer primary key, name text, score real);删除表 sqlitedrop table sqlitedrop table student查询表中所有记录 sqliteselect * from ; 按指定条件查询表中记录 sqliteselect * from where ; sqlite select * from student sqlite select * from student where name=zhaosqlite select * from student where name=zhao and score =95sqlite select count(*) from student where score90向表中添加新记录 sqliteinsert into values (value1, value2,);sqlite insert into student values(1, zhao, 92);按指定条件删除表中记录 sqlitedelete from where sqlite delete from student where scoreupdate set , where ; sqlite update student set score=0;sqlite update student set name=sun where no=3;在表中添加字段 sqlitealter table add column ; sqlite alter table student add column gender integer default 0;在表中删除字段 Sqlite中不允许删除字段,可以通过下面步骤达到同样的效果sqlite create table stu as select no, name, score from studentsqlite drop table studentsqlite alter table stu rename to student二、常用编程接口介绍1) int sqlite3_open(char *path, sqlite3 *db); 功能:打开sqlite数据库 path: 数据库文件路径 db: 指向sqlite句柄的指针 返回值:成功返回0,失败返回错误码(非零值) 2) int sqlite3_close(sqlite3 *db); 功能:关闭sqlite数据库 返回值:成功返回0,失败返回错误码 3) const char *sqlite3_errmg(sqlite3 *db); 返回值:返回错误信息 4) typedef int (*sqlite3_callback)(void *, int, char *, char *);int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *, char *errmsg); 功能:执行SQL操作 db:数据库句柄 sql:SQL语句 callback:回调函数errmsg:错误信息指针的地址返回值:成功返回0,失败返回错误码 不需要回调函数的情况:有关插入或更新的sql语句。if (sqlite3_exec(db, “delete from table1 where id = 1”, NULL, NULL, &errmsg) != SQLITE_OK)printf(“error : %sn”, errmsg);exit(-1);需要回调函数的情况:有关查询的sql语句。if (sqlite3_exec(db, “delete from table1 where id = 1”, NULL, NULL, &errmsg) != SQLITE_OK)int callback(void *para, int f_num, char *f_value, char *f_name)int i;printf(“*n”);for (i=0; if_num; i+)printf(“%s : %sn”, f_namei, f_valuei);return 0; if (sqlite3_exec(db, “select * from table”, callback, NULL, &errmsg) != SQLITE_OK)printf(“error : %sn”, errmsg);exit(-1);代码输出如下:no:2name:zhaoscore:86no:3name:wangscore:86不使用回调函数执行SQL语句:if (sqlite3_get_table(db, “select * from table”, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)printf(“error : %sn”, errmsg);exit(-1);index = ncolumn; / 第一条记录的第一个字段的下标 for (i=0; inrow; i+)for (j=0; jncolumn; j+)printf(“%s : %sn”, resultpj, resultpindex+); for (i=0; i(nr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论