目录
- 1)系统命令 以 '.' 开头
- 2)sql语句 以 ';'结尾
- 1. 创建表格
- 2. 插入数据
- 3. 查看数据库记录
- 4. 删除信息
- 5. 更新数据
- 6. 增加一列
- 7. 删除一列 (sqlite3 不支持直接删除一列)
- 3)sqlite3 数据库 C语言 API
- 1. 打开数据库
- 2. 关闭数据库
- 3. 错误信息
- 4. 执行一条sql语句
- 5. 查询回调函数
- 6. 查询函数
- 学生信息实例
1)系统命令 以 ‘.’ 开头
.help 帮助手册
.exit 退出
.table 查看当前数据库的有的表格名
.databases
.schema 查看表的结构属性
2)sql语句 以 ';'结尾
1. 创建表格
create table <table-name> (id integer, age integer, name char, score float);
2. 插入数据
insert into <table-name> values(1001, 15, zhangsan, 89);// 标准插入
insert into <table-name> values (id, age, name) values(1002, 18, lisi);//按列名插入
3. 查看数据库记录
select * from <table-name>; //查看全部
select * from <table-name> were age = 15;//按列名的信息 age=15 的查看
select * from <table-name> were age = 15 and name = lisi;//按多个列名的信息查看
select * from <table-name> were age = 15 or name = lisi;//age = 15 或 name = lisi满足一个条件就输出
select name, id from <table-name>;//指定字段查询
select * from <table-name> where score >= 90 and score <= 100;//查询score[90,100] 之间的信息
4. 删除信息
delete from <table-name> where id = 1005 and name = 'zhangsan';//删除id=1005和name=‘张三’的信息(同时满足)
delete from <table-name> where id = 1005 or name = 'zhangsan';//删除id=1005或name=‘张三’的信息(有一个条件满足就执行)
5. 更新数据
update <table-name> set name = 'wangwu' where id = 1002;//将id是1002的行信息中的name全部改成 'wangwu';
6. 增加一列
alter table <table-name> add column score float;//在<table-name>表名后面添加一列名: score属性为:float
7. 删除一列 (sqlite3 不支持直接删除一列)
1-- 创建一张新表create table <new-table-name> as select id, name, score from <table-name>;//从<table-name>表名中选择指定的列(id,name,score)为基础创建<new-table-name>新的表
2-- 删除原有的表drop table <table-name>;
3-- 将新表的名字改成原有的旧表的名字alter table <new-table-name> rename to <table-name>;//将<table-name>新表明改成<table-name>旧表明
3)sqlite3 数据库 C语言 API
1. 打开数据库
int sqlite3_open (const char *filename, /* Database filename (UTF-8) */sqlite3 **ppDb /* OUT: SQLite db handle */);功能:打开数据库参数:filename 数据库名称ppdb 数据库句柄返回值:成功为0 SQLITE_OK ,出错 错误码
2. 关闭数据库
int sqlite3_close(sqlite3* db);功能:关闭数据库参数:返回值:成功为0 SQLITE_OK ,出错 错误码
3. 错误信息
const char *sqlite3_errmsg(sqlite3*db);功能:得到错误信息的描述
4. 执行一条sql语句
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
5. 查询回调函数
查询回调函数:
int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name), /* Callback function */
功能:查询语句执行之后,会回调此函数
参数:arg 接收sqlite3_exec 传递来的参数ncolumns 列数f_value 列的值得地址f_name 列的名称
返回值:0,
6. 查询函数
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 */
);功能:不需要回调函数的查询函数
参数:db 数据库句柄zSql 存放sqlite3的语句pazResult 存放表中的数据pnRow 行pnColumn 列pzErrmsg 错误信息
小知识:如果结构体中定义的是一级指针,那么你要定义变量取地址的形式,如果是二级指针函数,你要定义一级指针取地址的形式
学生信息实例
student.c
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>#define N 128
#define DATABASE "student.db"
int do_insert(sqlite3 *db);
int do_query(sqlite3 *db);
int do_query_1(sqlite3 *db);
int do_delete(sqlite3 *db);
int do_update(sqlite3 *db);int main (int argc, char *argv[]) {sqlite3 *db;char *errmsg;int n;//打开SQlite数据库文件.dbif (sqlite3_open(DATABASE, &db) != SQLITE_OK) printf("%s\n", errmsg);else printf("open DATABASE success.\n");//创建一张数据库的表格 stuif (sqlite3_exec(db, "create table stu(id integer, name char, sex char, score integer);", NULL, NULL, &errmsg) != SQLITE_OK) {printf("%s\n", errmsg);} else {printf("Create of open table success.\n");}while (1) {printf("************************************************\n");printf("1: insert 2:query 3: delete 4: update 5: quit\n");printf("************************************************\n");printf("Please select:");scanf("%d", &n);getchar();switch(n) {case 1:do_insert(db);//插入信息break;case 2://do_query(db);//查询do_query_1(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 data n.\n");}}return 0;
}int do_insert(sqlite3 *db)
{int id;char name[32] = {};char sex;int score;char sql[N] = {};char *errmsg;printf("Input id: ");scanf("%d", &id);printf("Input name: ");scanf("%s", name);getchar();//回收掉垃圾字符printf("Input sex: ");scanf("%c", &sex);printf("Input score: ");scanf("%d", &score);sprintf(sql, "insert into stu values(%d, '%s','%c', %d )", id, name, sex, score );if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)printf("%s\n", errmsg);else printf("Insert done.\n");return 0;
}int callback(int* arg,int f_num ,char** f_value,char** f_name) {int i;for (i = 0; i < f_num; i++) {printf("%-8s", f_value[i]);}putchar(10);return 0;
}int do_query(sqlite3 *db)
{char sql[N] = {};char *errmsg;sprintf(sql, "select * from stu");if(sqlite3_exec(db, sql, callback, NULL, &errmsg) != SQLITE_OK)printf("%s\n", errmsg);else printf("Query done.\n");return 0;
}int do_query_1(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);elseprintf("query done.\n");int i = 0;int j = 0;int index = ncolumn;for(j = 0; j < ncolumn; j++){printf("%-10s", resultp[j]);}putchar(10);for(i = 0; i < nrow; i++){for(j = 0; j < ncolumn; j++){printf("%-10s ", resultp[index++]);}putchar(10);}return 0;
}int do_delete(sqlite3 *db)
{char sql[N] = {};char *errmsg;int id;printf("Invalid id:");scanf("%d", &id);getchar();sprintf(sql, "delete from stu where id = %d;", id);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)printf("%s\n", errmsg);else printf("Delete done.\n");return 0;
}int do_update(sqlite3 *db)
{char *errmsg;char sql[N] = {};char name[32] = "zhangsan";int id;printf("Input id:");scanf("%d", &id);sprintf(sql, "update stu set name = '%s' where id = %d;",name, id);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)printf("%s\n", errmsg);else printf("Delete done.\n");return 0;
}
输出结果: