```c
int sqlite3_open(const char *filename, sqlite3 **ppDb);
```
## 3.2 关闭数据库
```c
int sqlite3_close(sqlite3 *db);
```
## 3.3 执行sql语句
```c
int sqlite3_exec(sqlite3*,
const char *sql,
int (*callback)(void *, int, char **, char **),
void *arg,
char **errmsg);
```
## 3.4 获取结果内容
```c
int sqlite3_get_table(sqlite3 *db,
const char *zSql,
char ***pazResult,
int *pnRow,
int *pnColumn,
char **pzErrmsg);
```
## 3.5 获取错误信息
```c
const char *sqlite3_errmsg(sqlite3 *db);
```
## 3.6 释放表空间
```c
void sqlite3_free_table(char **result);
```
# 四、sqlite3语句
## 4.1 sqlite数据类型
```shell
NULL:标识一个NULL值
INTEGER(intger):整数类型 int
REAL(real):浮点数 float,double
TEXT(text):字符串 ""
CHAR(char):字符''
```
## 4.2 创建表
```shell
create if not exists table table_name(column1 type1, column2 type2, column3 type3……);
# 例如创建一个名为dictory的表(sheet),里面有word、translation等字段
create table if not exists table_name(word text, translation text);
# not null 指定列不允许为NULL值
create table if not exists student(student_name txt not NULL, id txt not NULL, sex char);
# default 在insert into语句没有提供特定的值时,为列提供一个默认值
create table if not exists student(student_name txt not NULL, id txt not NULL, sex char, grade integer default 99);
# unique 约束防止一个特定的列存在两个相同的值
create table if not exists student(student_name txt not NULL unique, id txt not NULL, sex char);
# check 约束 启用输入一条记录要检查值的条件。如果条件值为false,则记录违反了约束,且不能输入到表
create table if not exists student(student_name txt not NULL, id txt not NULL, sex char, grade intger check(grade > 0));
```
## 4.3 添加记录
```shell
insert into table_name(column1, column2, column3) values(val1, val2, val3);
insert into student(student_name, id, sex) values ('z3', '123', 'm');
或者
insert into student values('z3', '123', 'm');
```
## 4.4 查询
### 4.4.1 查看表结构
```shell
.schema
```
### 4.4.2 查看所有表
```shell
.tables
```
### 4.4.3 查看记录
```shell
# 获取所有数据记录
select * from table_name;
# 限制输出数据记录数量
select * from table_name limit val;
# 升序输出数据记录
select * from table_name order by column asc;
select * from dictory order by word asc;
# 降序输出数据记录
select * from table_name order by column desc;
select * from dictory order by word desc;
# 条件查询
select * from table_name where expression;
select * from table_name where column in ('val1', 'val2', 'val3');
select * from table_name where column = 'val1' or column = 'val2';
select * from table_name where column between val1 and val2;
select * from people where age>=10 and age <=15;
select name, age from people where age>= 10 and age<=15;
select * from table_name where column like 'xx%' or 'xxx_';
百分号(%)代表零个、一个或者多个数字或字符
下划线(_)代表一个单一数字或字符
```
## 4.5 修改数据记录
```shell
update table_name set column1=val1, column2=val2 where expression;
update student_info set id=0002, name=hencai where id=0001;
```
## 4.6 表中添加字段
```shell
alter table <table_name> add column <field> <type>;
alter table stu add column age integer;
```
## 4.7 删除数据记录
```shell
delete from table_name [where expression];
delete from stu where id=123;
```
## 4.8 删除表
```shell
drop table table_name;
drop table student;
```
## 4.9 删除指定字段
```shell
# step1:备份为temp id , name , age , sex
alter table info rename to temp;
# step2:创建新表
create table info(id, name, age);
#step3:导出到新表
insert into info select id, name, age from temp;
```