sqlite3 api&cli

1. Ubuntu上安装sqlite3

$ sudo apt-get install sqlite3 libsqlite3-dev

2. sqlite3常用命令

2.1创建或打开数据库

$ sqlite3 hi.db
sqlite>

    sqlite3一个文件就是一个数据库,如果hi.db不存在则创建一个新的数据库,如果存在则直接打开。然后进入sqlite3的交互界面。

2.2常用命令

sqlite>.help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
                         With no args, it turns EXPLAIN on.
.header(s) ON|OFF      Turn display of headers on or off
    .help命令打印出命令列表及对应的简要说明。sqlite3命令是可以简写的,只打前几个字母,如.h .he .hel .help是一样的命令。

sqlite>.exit
sqlite>.quit
    .exit与.quit都是退出,没有区别。

sqlite> .databases
seq  name             file
---  ---------------  ----------------------------------------------------------
0    main             /root/sqlite3_demo/hi.db
    .databases打印出当前数据库信息,名字与对应的文件路径名。

sqlite> .tables
student
    .tables打印出所有的关系表。

2.2备份与恢复

sqlite> .backup hi.bak
    .backup将当前数据库备份到hi.bak文件。

sqlite> .restore hi.bak
    .restore将数据库从hi.bak文件中恢复。

2.3导入与导出

sqlite> .output hi.sql
sqlite> .dump
    .output将输出重定向到hi.sql文件。
    .dump将当前数据库导出成sql语句组成的文件。

sqlite> .read hi.sql
    .read读取并执行hi.sql中的SQL语句。

2.4显示控制

sqlite> .show
     echo: off
  explain: off
  headers: off
     mode: list
nullvalue: ""
   output: stdout
separator: "|"
    stats: off
    width:
    .show打印出当前的显示控制选项。

sqlite> .echo on
sqlite> .tables
.tables
student
    .echo打开回显,打印出执行的命令。

sqlite> select * from student;
name  age
----  -------------
shy   26
sqlite> .headers off
sqlite> select * from student;
shy   26
    .headers是否显示表头。

sqlite> .stats on
sqlite> select * from student;
shy   26
Memory Used:                         79224 (max 91080) bytes
Number of Outstanding Allocations:   112 (max 165)
Number of Pcache Overflow Bytes:     6552 (max 10920) bytes
Number of Scratch Overflow Bytes:    0 (max 0) bytes
Largest Allocation:                  64000 bytes
Largest Pcache Allocation:           1160 bytes
Largest Scratch Allocation:          0 bytes
Lookaside Slots Used:                0 (max 0)
Successful lookaside attempts:       0
Lookaside failures due to size:      0
Lookaside failures due to OOM:       0
Pager Heap Usage:                    6352 bytes
Page cache hits:                     2
Page cache misses:                   0
Schema Heap Usage:                   1168 bytes
Statement Heap/Lookaside Usage:      1960 bytes
Fullscan Steps:                      0
Sort Operations:                     0
Autoindex Inserts:                   0
    .stats打开统计信息。

sqlite> .mode html
sqlite> select * from student;
<TR><TD>shy</TD>
<TD>26</TD>
</TR>
sqlite> .mode csv
sqlite> select * from student;
shy,26
sqlite> .mode tabs
sqlite> select * from student;
shy     26
sqlite> .mode insert
sqlite> select * from student;
INSERT INTO table VALUES('shy',26);

    .mode控制显示的格式。

3.sqlite3常用编程接口

3.1数据库的开关与创建

#include <sqlite3.h>
int sqlite3_open(const char *filename, sqlite3 **ppDb);
int sqlite3_close(sqlite3 *pDb);
    sqlite3_open打开或创建一个数据库,filename是文件名的字符串,ppDb是数据库结构的二级指针,作返回值。
    sqlite3_close关闭数据库,pDb数据库结构的指针。

3.2错误的显示

<pre name="code" class="cpp">#include <sqlite3.h>
int sqlite3_errcode(sqlite3 *db);const char *sqlite3_errmsg(sqlite3 *db);
     sqlite3_errcode返回数据库的错误码。 
    sqlite3_errmsg返回数据库的错误字串描述。

3.3SQL语句的执行

int sqlite3_exec(sqlite3 *db, const*sql,
               int (*callback)(void *arg, int num, char **data, char **head),
               void *arg, char **errmsg);
    sqlite3_exec在db数据库里执行sql语句,如果是查询语句,每查到一行数据就调用一次callback,如果有错误就将错误的文字信息放到errmsg指向的空间。
    callback函数的arg参数就是调用sqlite3_exec时的参数arg,这个参数可以为每一次处理存储私有数据。num就是每一行数据有几列,类似main函数的argc;data就是每行的数据,head就是表头,类似main函数的argv。

3.4源码示例

#include <stdio.h>
#include <sqlite3.h>


int show(void *flag, int num, char **row, char **info)
{                                                        //查询结果有num个字段
        if (!*flag) {                                    //打印表头只打一次
                printf("%s\t%s\n", info[0], info[1]);
                *flag = 1;
        }
        printf("%s\t%s\n", row[0], row[1]);              //打印每行数据
        return SQLITE_OK;
}

int main(int argc, const char *argv[])
{
        sqlite3 *sql;
<pre name="code" class="cpp"><span style="white-space:pre">	</span>int flag = 0;
sqlite3_open("hi.sql", &sql); //创建了一个数据库 sqlite3_exec(sql, "create table stu(name char, age int)", NULL, NULL, NULL); //创建了一个关系表 sqlite3_exec(sql, "insert into stu(name, age) values('shy', 27)", NULL, NULL, NULL); //插入了一行数据 sqlite3_exec(sql, "select * from stu", show, &flag, NULL); //查询出所有行的数据 sqlite3_close(sql); //关闭数据库 return 0;}
     运行结果如下 
$ gcc hi.c -o hi -lsqlite3
$ ./hi
name    age
shy     27
shy     27

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值