sqlite数据库

sqlite轻量级数据库

1 数据库的安装

 sudo dpkg -i  *.deb

2 数据库命令

1.系统命令 , 都以’.'开头

  .help
  .exit 
  .quit
  .table   查看表
  .schema  查看表的结构
  .database 查看打开的数据库
  .table 查看当前数据库下的表格

2.sql语句

1.创建一张表

 /*创建一张表格 表名();*/
create table stuinfo(id integer, name text(char), age integer, score float);      

2. 插入一条记录

/*所有字段均有插入*/
insert into stuinfo values(1001, 'zhangsan', 18, 80);
/*插入部分字段*/
insert into stuinfo (id, name, score) values(1002, 'lisi', 90);

3. 查看数据库记录

/*从表中查询数据库表所有字段*/
  select * from stuinfo;	
/*指定条件查询*/
  select * from stuinfo where score = 80;
  select * from stuinfo where score = 80 and name= 'zhangsan';
  select * from stuinfo where score = 80 or name='wangwu';
  select * from stuinfo where score >= 85 and score < 90;
/*查询数据库表中部分字段*/
  select name,score from stuinfo;  查询指定的字段

4. 删除一条记录

delete from stuinfo where id=1003 and name='zhangsan';

5. 更新一条记录

update stuinfo set age=20 where id=1003;
update stuinfo set age=30, score = 82 where id=1003;

6. 删除一张表

drop table stuinfo;

7. 增加一列

alter table stuinfo add column sex char;

8. 删除一列

sqlite3不支持,直接删除一列

1.创建一张新表
creat table 新表名 as 不删除的列(逗号隔开列名)from 旧表名
2.删除原有旧表
drop table 旧表名
3.将新表改成原有的旧表的名字
alter table 新表名 rename to 旧表名

/******************************/
create table stu as select id, name, score from stuinfo;  //创建一张表,从原有表中提取数据
drop table stuinfo; //删除原有表
alter table stu rename to stuinfo; //旧表改变名字
/******************************/

sqlite3 数据库 C语言 API

int sqlite3_open(
      const char *filename,   /* Database filename (UTF-8) */
      sqlite3 **ppDb          /* OUT: SQLite db handle */
     );
    功能:打开数据库
    参数:filename  数据库名称
          ppdb      数据库句柄
    返回值:成功为0 SQLITE_OK ,出错 错误码

在这里插入图片描述

int sqlite3_close(sqlite3* db);
    功能:关闭数据库
    参数:
    返回值:成功为0 SQLITE_OK ,出错 错误码

在这里插入图片描述

const char *sqlite3_errmsg(sqlite3*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
查询回调函数:
int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name),  /* Callback function */
功能:查询语句执行之后,会回调此函数
参数:arg   接收sqlite3_exec 传递来的参数
      ncolumns 列数
      f_value 列的值得地址
      f_name   列的名称
返回值:0
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 */
);
void sqlite3_free_table(char **result);

查询

SQLite编程接口
int   sqlite3_open(char  *path,   sqlite3 **db);
功能:打开sqlite数据库
path:数据库文件路径
db:指向sqlite句柄的指针(通过指针操作函数)
返回值:成功返回0,失败返回错误码(非零值)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值