【1】数据库
sudo dpkg -i *.deb
系统命令, 以‘.’开头
.help 帮助
.exit 退出
.quit 退出
.schema 查看表的结构
.table 查看表的信息
sql语句, 以 ';'结尾
创建一张表
create table stu(id int, name char, score int);
插入一条记录
insert into stu values(1001, "zhangsan", 80);
查看表中记录
select * from stu;
select * from stu where id=1001;
select * from stu where id=1001 or id=1003;
select * from stu where name='zhaoliu' and score = 56;
select * from stu where score >= 60;
select * from stu where score <= 60;
删除一条记录
delete from stu where id=1003;
更新一条记录
update stu set score=60 where id=1002;
添加一列
alter table stu add column address char;
删除一列 (sqlite 不支持直接删除一列)
create table stu1 as select id, name ,score from stu;
drop table stu;
alter table stu1 rename to stu;
删除一张表
drop table stu;
主键
create table stu (name text primary key, score int);
【2】sqlite C 语言API
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
功能:打开或者创建一个数据库
参数:filename 数据库的名字
ppdb 数据库的句柄
返回值:成功 SQLITE_OK ,出错 error_code
const char *sqlite3_errmsg(sqlite3* db);
功能:获取错误信息
参数:db 数据操作的指针
返回值:成功 错误信息的首地址
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 , 出错 error_code
回调函数:
int (*callback)(void* arg,int column,char** f_value,char** f_name)
功能:得到查询的结果
参数:arg exec函数传递进来的参数
column 列数量
f_value 列的值
f_name 列的名称
返回值:应该为0
**************************************************************************************
int fun()
{
sum(a, b); // sub(a, b)
}
int sub(int a, int b)
{
return a-b;
}
// 模拟一个回调函数
int fun(int (*p)(int a, int b), int m, int n)
{
int ret;
re = (*p)(m, n);
...
return ret;
}
fun(add, 3, 5);
fun(sub, 3, 5);
****************************************************************************************
int sqlite3_close(sqlite3* db);
功能:关闭数据库
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 */
);
功能:查询sql数据库
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 */
);
功能:执行一个sql语句
参数:db 打开的数据库的句柄指针
sql 将要执行的sql语句
azResult 查询的结果
nRow 记录的行数
nColumn 记录的列数
errmsg 存储的是错误的信息
返回值:成功 SQLITE_OK
void sqlite3_free_table(char **result);
功能:释放资源
sudo dpkg -i *.deb
系统命令, 以‘.’开头
.help 帮助
.exit 退出
.quit 退出
.schema 查看表的结构
.table 查看表的信息
sql语句, 以 ';'结尾
创建一张表
create table stu(id int, name char, score int);
插入一条记录
insert into stu values(1001, "zhangsan", 80);
查看表中记录
select * from stu;
select * from stu where id=1001;
select * from stu where id=1001 or id=1003;
select * from stu where name='zhaoliu' and score = 56;
select * from stu where score >= 60;
select * from stu where score <= 60;
删除一条记录
delete from stu where id=1003;
更新一条记录
update stu set score=60 where id=1002;
添加一列
alter table stu add column address char;
删除一列 (sqlite 不支持直接删除一列)
create table stu1 as select id, name ,score from stu;
drop table stu;
alter table stu1 rename to stu;
删除一张表
drop table stu;
主键
create table stu (name text primary key, score int);
【2】sqlite C 语言API
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
功能:打开或者创建一个数据库
参数:filename 数据库的名字
ppdb 数据库的句柄
返回值:成功 SQLITE_OK ,出错 error_code
const char *sqlite3_errmsg(sqlite3* db);
功能:获取错误信息
参数:db 数据操作的指针
返回值:成功 错误信息的首地址
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 , 出错 error_code
回调函数:
int (*callback)(void* arg,int column,char** f_value,char** f_name)
功能:得到查询的结果
参数:arg exec函数传递进来的参数
column 列数量
f_value 列的值
f_name 列的名称
返回值:应该为0
**************************************************************************************
int fun()
{
sum(a, b); // sub(a, b)
}
int sub(int a, int b)
{
return a-b;
}
// 模拟一个回调函数
int fun(int (*p)(int a, int b), int m, int n)
{
int ret;
re = (*p)(m, n);
...
return ret;
}
fun(add, 3, 5);
fun(sub, 3, 5);
****************************************************************************************
int sqlite3_close(sqlite3* db);
功能:关闭数据库
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 */
);
功能:查询sql数据库
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 */
);
功能:执行一个sql语句
参数:db 打开的数据库的句柄指针
sql 将要执行的sql语句
azResult 查询的结果
nRow 记录的行数
nColumn 记录的列数
errmsg 存储的是错误的信息
返回值:成功 SQLITE_OK
void sqlite3_free_table(char **result);
功能:释放资源