安装
Ubuntu安装sqlite3:
sudo apt-get install sqlite3 libsqlite3-dev
使用sqlite3命令行
参考:https://www.sqlite.org/quickstart.html
1)创建名字为‘test.db’的数据库
$ sqlite3 test.db
2)创建表 (参考sql语法)
sqlite> create table user
...> (
...> id integer,...> name varchar(20)
...> );
3)插入记录到user表
sqlite> insert into user values(1,"ubuntu");
4)查询user表中的记录
sqlite> select * from user;
1|ubuntu
使用sqlite3的c/c++接口的例子:
- 源文件:
// main.c
#include <stdio.h>
#include <sqlite3.h>
static int callback(void *NotUsed,int argc,char **argv,char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n",azColName[i],argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc,char **argv){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=3 ){
fprintf(stderr,"Usage: %s DATABASE sql-STATEMENT\n",argv[0]);
return(1);
}
rc = sqlite3_open(argv[1],&db);
if( rc ){
fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}
rc = sqlite3_exec(db,argv[2],callback,0,&zErrMsg);
if( rc!=sqlITE_OK ){
fprintf(stderr,"sql error: %s\n",zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
- 编译方法:
不要忘记连接sqlite3库文件 -lsqlite3
$ gcc main.c -o main -lsqlite3
- 运行:
$ ./main test.db "select * from user" id = 1 name = ubuntu
原文链接:https://www.f2er.com/sqlite/199361.html