初学sqlite3数据库

一、概述
sqlite3数据库是一个较为精简的数据库,使用方便,现在大多嵌入式设备上都在使用它。使用起来也比较简单。它有以下几个优点:
1、以文件的形式存放于磁盘中;
2、可以实现在不同大小端字节序机器之间的共享;
3、比目前大多数据库操作要快;
安装:

leihaojie@Ubuntu-14:~$ sudo apt-get install sqlite

二、sqlite3系统命令

 .help 	//帮助(显示所有命令)
 .exit 	//退出
 .quit   //退出
 .table  //查看当前数据库下所有表
 .schema //查看表的结构

创建新表

sqlite> create table stu(id integer,name char,socre integer);

添加数据

sqlite>insert into <table_name> values (value1, value2,);

查询记录

sqlite>select * from <table_name>; //查询所有;
select name,score from <table_name>; //查询指定目标;

更新记录

sqlite>update <table_name> set <f1=value1>, <f2=value2>… where < expression>;

删除目标

sqlite> delete from <table_name> where id=888 and name=‘laoba’; //要添加where +指定目标不然整张表会被删除

删除表格

sqlite>drop table <table_name>

三、sqlite3API操作函数
int sqlite3_open()

int sqlite3_open(
const char *filename, //数据库名称(字符串类型),一般存放在当前路径下(./)
sqlite3 **ppDb  //sqilte3结构二级指针,数据库连接对象
)

返回值:成功则返回SQLITE_OK,并且ppDb指向新的数据库连接。其他值则表示失败。
例:

int base= sqlite3_open("test.db", &db);
if( base!=SQLITE_OK)
{
   printf("open bd error: %s\n", sqilte3_errmsg(db));
   sqilte_close(db);
   return -1;
}

int sqlite3_exec()

int sqlite3_exec(
sqlite3*, //已经打开的数据库
const char *sql, //要执行的SQL语句(字符串)
int (*callback)(void*, int, char**, char**), //回调函数
void*, //作为回调函数的第一个函数
char **errmsg, //错误信息
)

SQL语句分为两类:
1、不需要返回结果情况的,就不需要回调函数,用NULL就行。
例:insert, create, update, delete。
2、如果有数据返回,那么需要通过回调函数去获取,每查到一条记录就需要调用一次回调函数。
例:select。

回调函数:

int (*callback)(void* , //传回给回调函数的参数,
int//记录字段的个数,
char**,字段值(一维数组),
char**//字段名(一维数组)
)

只用于查询,查询结果是一个函数指针类型,传递一个参数名即可;

错误信息:

const char *sqlite3_errmsg(sqlite3*);

通过句柄 db,得到数据库操作错误信息;返回值:错误信息的首地址

关闭数据库:

int sqlite3_close(sqlite3*);

部分功能实现:
添加

  int insert(sqlite3 *db)
  {
          int id;
          char name[32];
          int score;
          char sql[128];
          char *errmsg;
  
  
          printf("input id:");
          scanf("%d", &id);
          printf("input name:");
          scanf("%s", name);
          printf("input score:");
          scanf("%d", &score);

 
          sprintf(sql,"insert into stu values(%d, '%s', %d);", id, name, score);
  
          if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
          {
                 printf("%s\n", errmsg);
          }
          else
          {
                  printf("inset done.\n");
          }
  
          return 0;
  }

删除:

 int delete(sqlite3 *db)
  {
          int id;
          char  sql[128];
          char  *errmsg;
  
          printf("input id:");
          scanf("%d", &id);
  
          sprintf(sql,"delete from stu where id=%d", id);
          if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
          {
                  printf("%s\n", errmsg);
          }
         else
          {
                  printf("delete done\n");
          }
  
          return 0;
   }

修改:

int update(sqlite3 *db)
 {
         int   id;
         int   score;
         char   sql[128];
         char   *errmsg;
 
         printf("input update id:");
         scanf("%d", &id);
  
         printf("input update score:");
         scanf("%d", &score);
  
         sprintf(sql, "update stu set score=%d where id=%d;", socre, id);
         if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
         {
                 printf("%s\n", errmsg);
         }
         else
         {
                 printf("update success.\n");
         }
         return 0;
 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值