Sqlite的编译、使用和调试

首先准备好gcc的编译环境,这里我用的是mingw。

然后下载sqlite3的源代码

然后用如下的命令进行编译:


gcc -DsqlITE_DEBUG -ggdb *.c -o sqlite3

其中的编译参数“sqlITE_DEBUT”是过会用来理解sqlite时使用的,详细请参考附录。

编译参数-ggdb是用来生成gdb调试信息的,在待会源代码的调试中会用到。


接着运行编译好的sqlite3.exe


在命令行中首先打开调试属性(用于查看sqlite的虚拟机的指令集)

PRAGMA vdbe_trace=ON;

最后就是运行sqlite语句并分析。


sqlite> create table cust(id int not null primary key,name char(30));
sqlite> PRAGMA vdbe_trace=ON;
VDBE Execution Trace:
   0 Expire           0    0    0      00
   1 Halt             0    0    0      00
sqlite> insert into cust values(21,"harry");
VDBE Execution Trace:
   0 Trace            0    0    0      00
   1 Goto             0   20    0      00
  20 Transaction      0    1    0      00
  21 VerifyCookie     0    1    0      00
  22 TableLock        0    2    1 cust 00
  23 Goto             0    2    0      00
   2 OpenWrite        0    2    0 2    00 cust
   3 OpenWrite        1    3    0 keyinfo(1,BINARY) 00 sqlite_autoindex_cust_1
   4 NewRowid         0    2    0      00
REG[2] =  i:1
   5 Integer         21    3    0      00
REG[3] =  i:21
   6 String8          0    4    0 harry 00
REG[4] =   t5[harry](8)
   7 HaltIfNull      19    2    3 cust.id may not be NULL 00
REG[3] =  i:21
   8 Scopy            3    5    0      00
REG[3] =  i:21
REG[5] =  i:21
REG[5] =  i:21
   9 Scopy            2    6    0      00
REG[2] =  i:1
REG[6] =  i:1
REG[6] =  i:1
  10 MakeRecord       5    2    1 db   00
REG[1] =  z5[0301011501.....](8)
  11 Scopy            2    7    0      00
REG[2] =  i:1
REG[7] =  i:1
REG[7] =  i:1
  12 IsUnique         1   14    7 5    00
REG[7] =  i:1
  14 IdxInsert        1    1    0      10
REG[1] =  z5[0301011501.....](8)
  15 MakeRecord       3    2    7 da   00
REG[7] =  z9[030117156861727279....harry](8)
  16 Insert           0    7    2 cust 1B
REG[7] =  z9[030117156861727279....harry](8)
REG[2] =  i:1
  17 Close            0    0    0      00
  18 Close            1    0    0      00
  19 Halt             0    0    0      00
sqlite>


上面的调试信息在以后的文章中介绍sqlite的虚拟机的时候会仔细讲解。

==================================

接下来,将用介绍用eclipse结合gdb来调试sqlite的源代码

eclipse的环境要求:安装插件cdt。

在环境变量中要有gcc和gdb这两个命令。

新建一个c工程,然后把sqlite的几个代码导入到工程中。


调试开始:

1. 打开sqlite.exe

2. 右击eclipse的c工程,然后在弹出的菜单上选择"Debug As"/"Run Configurations...",在弹出的对话框的左边双击“C/C++ Attach to Application”,并且在右边选择“disable auto build”,如下图所示:

3. 点击Debug按钮,弹出如下对话框,选择刚刚启动的sqlite.exe,就进入调试了。

4. 加入断点设置在shell.c的process_input函数中(此函数用来接收控制台的输入),然后在控制台输入sql命令,调试的画面如下:

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能