数据库操作

1、数据库是一个有结构、集成的、可共享的统一管理的数据集合
2、数据库的三种模式:内模式、模式、外模式
3、关系:一个关系就是一张二维表
     元祖:表中的行称为元祖,一行就是一个元祖
     属性:一列为一个属性
     域:属性的取值范围
4、创建数据库
(1)文件格式:text.db
sqlite>sqlite3 text.db
sqlite>create table test(id primary key,value text)
(2)查看数据
sqlite>select *from text;
(3)插入数据
sqlite>insert into text(id,name)values(1,'zhang');
sqlite>insert into text(id,name)values(2,'wang');
(4)生成注明
sqlite>.head on
sqlite>.mode column
(5)添加索引
sqlite>create index test_index on text(name);
(6)更新数据
sqlite>update text set age=19 where name='zhang';
(7)顺序输出
sqlite>select * from text order by name src;
(8)逆序输出
sqlite>select * from text order by name desc;
(9)查询表中人的姓名
sqlite>select name from text
(10)确定范围
使用between...and..
(11)查询第二个字为晗的名字
sqlite>select name from text where name like '_晗%';


数据库函数
1、sqlite3_open
函数的作用:打开一个数据库
函数的原型:int sqlite3_open(const char *dbname,sqlite3 **db);
函数的参数:dbname:数据库的名称;db:数据库的句柄
返回值:操作成功:SQLITE_OK
2、sqlite3_close
函数的作用:关闭数据库
函数的原型:int sqlite3_close(sqlite3 *db);
3、sqlite3_exec
函数的作用:执行SQL语句
函数的原型:int sqlite2_exec(sqlite3 *db,const char *sql,int (*callback)(void*,int,char **,char **),void *para,char **errmsg)
函数的参数:db:数据库句柄;sql:SQL语句;callback:回调函数;para:传入的参数;errmsg:错误信息
4、sqlite3_get_table
函数的作用:执行sql的查询功能
函数的原型:int sqlite3_get_table(sqlite *db,const char *zSql,char ***pazResult,int *pnRow,int *pnColumn,char **pzErrmsg);
函数的参数:db :数据库句柄;zSql:sqlite3语句,pazResult:查询的结果;pnRow:结果集的行数;pnColumn:结果集的列数;pzErrmsg:错误信息


回调函数
typedef int (*sqlite_callback)(void *para,int columnCount,char **columnValue,char **columnName);
函数的作用:执行查询的结果
函数的参数:para:exec函数传入的参数指针
                    columnCount:查询到的这个记录的字段数
                    columnValue:保存查询到的数据
                    columnName:字段名称

你可能感兴趣的:(数据库操作)