fragment/常用函数/sql_functions.c
2021-08-29 00:02:47 +08:00

68 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//integer:带符号整形最多64位
//real:8字节浮点
//text:字符,无大小限制
//bolb:任意类型,无大小限制
//null:空值
//! sqlite3 *.db 创建或打开*.db文件
//! .quit或.exit 退出数据库
//create table 表名称 (列名称 数据类型,……); 创建表
//primary key 主键,每个表只能有一个,其列数据不可重复
//.tables 查看表
//.schema 查看表结构
//alter table 表名 add 列名 数据类型; 添加新列
//alter table 表名 rename to 新表名; 修改表名
//drop table 表名称; 删除表
//insert into 表名 values (列值……); 插入新行并赋值,字符串加‘’号
//insert into 表名 (列名……) values (列值……); 插入新行部分赋值
//update 表名 set 列……=值…… 匹配条件; 修改where匹配行的列值空匹配所有
//delete from 表名 匹配条件; 删除匹配行
//select * from 表名 匹配条件;
//select 列名…… from 表名 匹配条件; 从表中选取数据
//.mode colum 左对齐列
//.headers on 列名显示
//where 列名 in (列值……) 在where子句中规定多个值
//where 列1=值1 and 列2=值2 and ... 多个条件与
//where 列1=值1 or 列2=值2 or ... 多个条件或
//where 列名 between A and B 选取A、B之间的数据范围
//where 列名 like 列值 数字相当于“=”,字符串可用“%”通配
//where 列名 not in/between/like ...
//where not 列1=值1 and/or 列2=值2 and/or ... 原结果集补集
//order by 列名 (desc) 根据指定列对结果集排序用dest降序
//begin; 开始一个事务
//commit; 确认begin后的全部命令
//rollback; 取消begin后的所有操作
int sqlite3_open(char *db_name,sqlite3 **db); //打开数据库成功返回SQLITE_OK
int sqlite3_close(sqlite3 *db); //关闭数据库成功返回SQLITE_OK链接时添加 -lpthread 和 -ldl两个库
int sqlite3_exec(sqlite3 *db,const char *sql,exechandler_t callback,void *arg,char **errmsg); //执行sql语句若结果集不空回调下面callback
typedef int (*exechandler_t)(void *para,int n_column,char **column_value,char **column_name);
int sqlite3_get_table(sqlite3 *db,const char *sql,char ***resultp,int *nrow,int *ncolumn,char **errmsg); //执行sql语句结果集保存在resultp中
void sqlite3_free_table(char **resultp); //释放sqlite3_get_table分配的内存
//函数
//length() 返回字符串长度
//lower() 将字符串转换为小写
//upper() 将字符串转换为大写
//select 函数名(列名) from 表名;
//聚集函数
//avg() 返回某列的平均值
//count() 返回某列的行数
//max() 返回某列的最大值
//min() 返回某列的最小值
//sum() 返回某列值之和
//select 列名…… from 表名 group by 列名; 根据某一属性重新分组出现在where子句之后
//select 函数名 (列名) …… from 表名 group by 列名 having 函数名 (列名) 限制值; 查看满足having条件的分组
//主键惟一标识一行,对用户无意义,常用语索引,勿更新,无动态变化数据,由计算机自动生成
//unique 用法同主键,保证一个列中数据惟一,可以有多个,可以修改和更新
//create table 表名 (列名 数据类型 check (判断语句)); 保证某列数据满足一定条件
//select 列名…… from 表…… where 判断语句; 用“.”分隔表名和列名
//create view 视图名 as 语句; 创建视图
//drop view 视图名; 删除视图
//create trigger 触发器名 before/after insert/update/delete on 表名
//begin 语句; end;
//datetime('now') 获取当前系统时间
//.tables 查看触发器
//drop trigger 触发器名; 删除触发器
//create index 索引名 on 表名(列名); //创建索引
//.indices 查看索引
//drop index 索引名; 删除索引