一、sqlite3在linux下安装
下载 sqlite-autoconf-*.tar.gz压缩文件,并上传至虚拟机。
随后依次输入以下命令完成安装:
$ tar xvzf sqlite-autoconf-3071502.tar.gz
$ cd sqlite-autoconf-3071502
$ ./configure --prefix=/usr/local
$ make
$ make install
安装完成后在命令行输入sqlite3验证是否安装完成:
输入命令,退出数据库
.help
二、sqlite命令
系统命令
以.+命令组合的方式出现,例如在命令行输入.help可以展示所有的系统命令:
常用的系统命令:
.help --打印所有的系统命令
.quit --退出
.exit --退出
.schema --展示所有表
sql命令 :不以.开头,通常以;结尾
①创建一张数据表 stu
create table stu(id Integer, name char, score Integer);
创建一个stu表,表内参数格式为Integer,char和Integer,通过系统命令.schema可以查看
② 向表中插入数据
insert into stu values(1, "张三", 100); --插入一个完整的数据
insert into stu (name, score)values("李二", 85); --插入部分数据
插入一个部分数据,需要指定插入的数据名。
③ 查询记录
select * from stu; --查询所有字段
select name from stu; --查询数据库的name字段
select * from stu where name="张三"; --查询name字段为张三的数据
select * from stu where name="张三" or score=85; --查询name字段为张三或score字段为85的数据
④ 删除一条记录
delete from stu where name="张三"; --删除name字段为张三的数据
⑤ 更新一条数据
update stu set id=2 where name="李二"; --将name字段为第二的数据的id更新为2
⑥ 插入新的字段
alter table stu add column address char; --将address字段插入到stu中
⑦ 删除一列
sqlite3不支持直接删除一列数据,但可以通过一下方式达到同样的效果。
1.创建一张新表
create table stu1 as select id, name, score from stu;
2.删除旧表
drop table stu;
3.将新表的名字更改为旧表的名字
alter table stu1 rename to stu;
三、调用C_API操作数据库
① sqlite3_open函数
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
功能:打开数据库
参数:filename 数据库名称
ppdb 数据库句柄
返回值:成功为0 SQLITE_OK ,出错 错误码
② sqlite3_close函数
int sqlite3_close(sqlite3* db);
功能:关闭数据库
参数:
返回值:成功为0 SQLITE_OK ,出错 错误码
③ sqlite3_errmsg函数
const char *sqlite3_errmsg(sqlite3*db);
功能:得到错误信息的描述
④ sqlite3_exec函数
int sqlite3_exec(
sqlite3* db, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void* arg,int,char**,char**), /* Callback function */
void * arg, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
功能:执行一条sql语句
参数:db 数据库句柄
sql sql语句
callback 回调函数,只有在查询时,才传参
arg 为回调函数传递参数
errmsg 错误消息
返回值:成功 SQLITE_OK
查询回调函数:
int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name), /* Callback function */
功能:查询语句执行之后,会回调此函数
参数:arg 接收sqlite3_exec 传递来的参数
ncolumns 列数
f_value 列的值得地址
f_name 列的名称
返回值:0,
⑤ sqlite3_get_table函数
int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);
⑥ void sqlite3_free_table函数
void sqlite3_free_table(char **result);
简易学生信息管理系统
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#define DATABASEPATH "./student.db" //数据库路径
//往数据库中插入信息
int do_insert(sqlite3 *db)
{
int id; //插入的学生id
char name[32]; //插入的学生姓名
char sex; //插入学生的性别
int score; //插入学生的分数
char *errmsg; //保存错误码
char cmd[128]; //保存命令
printf("Input:id:");
scanf("%d", &id); //此时会遗留一个\n,在缓冲区
printf("Input:name:");
scanf("%s", name);
getchar(); //取出缓冲区的\n
printf("Input sex:");
scanf("%c", &sex);
printf("Input score:");
scanf("%d", &score);
sprintf(cmd, "insert into stu values(%d, '%s', '%c', %d)", id, name, sex, score);
if(sqlite3_exec(db, cmd, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
{
printf("Insert done.\n");
}
return 0;
}
//从数据库删除内容
int do_delete(sqlite3 *db)
{
int id;
char cmd[128]; //保存命令
char *errmsg;
printf("Input id:");
scanf("%d", &id);
sprintf(cmd, "delete from stu where id = %d", id);
if(sqlite3_exec(db, cmd, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
{
printf("Delete done.\n");
}
return 0;
}
//从数据库更新内容
int do_update(sqlite3 *db)
{
int id;
char cmd[128];
char name[32] = "zhangsan";
char *errmsg;
printf("Input id:");
scanf("%d", &id);
sprintf(cmd, "update stu set name='%s' where id=%d", name,id);
if(sqlite3_exec(db, cmd, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
{
printf("update done.\n");
}
return 0;
}
int do_query1(sqlite3 *db)
{
char *errmsg;
char ** resultp;
int nrow;
int ncolumn;
if(sqlite3_get_table(db, "select * from stu", &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
return -1;
}
else
{
printf("query done.\n");
}
int i = 0;
int j = 0;
int index = ncolumn;
//resultp保存每列的结果,前ncolumn个内容是字段属性id name sex score
for(j = 0; j < ncolumn; j++)
{
printf("%-10s ", resultp[j]);
}
putchar(10);
for(i = 0; i < nrow; i++) //nrow行数据
{
for(j = 0; j < ncolumn; j++) //每列的内容
{
printf("%-10s ", resultp[index++]);
}
putchar(10);
}
return 0;
}
int main(int argc, char *argv[])
{
sqlite3 *db; //定义一个句柄
char *errmsg; //保存错误码
int n;
if(sqlite3_open(DATABASEPATH, &db) != SQLITE_OK) { //打开失败,进行报错
printf("%s\n", sqlite3_errmsg(db));
return -1;
} else {
printf("DATABASE open success\n");
}
//如果不存在表stu,则进行创建
if(sqlite3_exec(db, "create table if not exists stu(id int, name char , sex char , score int);",
NULL, NULL, &errmsg) != SQLITE_OK) {
printf("%s\n", errmsg);
} else {
printf("Create or open stu success\n");
}
//学生信息表有4个操作,插入 查询 删除 更新
//循环对输入进行处理
while(1) {
printf("********************************************\n");
printf("1: insert 2:query 3:delete 4:update 5:quit\n");
printf("********************************************\n");
printf("Please select:");
scanf("%d", &n);
switch(n) {
case 1:
do_insert(db);
break;
case 2:
do_query1(db);
break;
case 3:
do_delete(db);
break;
case 4:
do_update(db);
break;
case 5:
printf("main exit.\n");
sqlite3_close(db);
exit(0);
break;
default:
printf("Invalid input\n");
}
}
return 0;
}