SQLite实现了大部分SQL92标准的SQL语句,同时支持ACID。还有其它许多特性这里不做深究,因为这在嵌入式领域来说应该是够用了。
下载:
下载页面:http://www.sqlite.org/download.html
我使用的还是当前最新版本:sqlite-3.3.7.tar.gz
(写完的时候已经更新出3.3.8版本了,真快啊……)
安装:
时间不多,简单介绍安装过程:
解压sqlite到uclinux-dist/user/sqlite/
============ 对uClinux的修改 ============
1. 下载sqlite,解压到uclinux-dist/user/下
2. 编辑uclinux-dist/user/下的Makefile,增加:
dir_$(CONFIG_USER_SQLITE_SQLITE) += sqlite
3. uclinux-dist/config/Configure.help中增加:
CONFIG_USER_SQLITE_SQLITE SQLite Database.
4. uclinux-dist/config/config.in最后增加:
mainmenu_option next_comment comment 'Database' bool 'sqlite' CONFIG_USER_SQLITE_SQLITE endmenu
============ 对SQLite的修改 ============
1. uclinux-dist/user/sqlite/main.mk中:
TCCX修改为:
TCCX = $(TCC) $(OPTS) $(THREADSAFE) $(USLEEP) -I. -I$(TOP)/src $(CFLAGS)
LIBOBJ修改为(一些模块不需要,比如tcl):
LIBOBJ+= alter.o analyze.o attach.o auth.o btree.o build.o callback.o complete.o date.o delete.o expr.o func.o hash.o insert.o loadext.o main.o opcodes.o os.o os_unix.o pager.o parse.o pragma.o prepare.o printf.o random.o select.o table.o tokenize.o trigger.o update.o util.o vacuum.o vdbe.o vdbeapi.o vdbeaux.o vdbefifo.o vdbemem.o where.o utf.o legacy.o vtab.o
sqlite3$(EXE)规则部分修改为:
shell.o: $(TOP)/src/shell.c sqlite3.h $(TCCX) $(READLINE_FLAGS) -c $(TOP)/src/shell.c sqlite3$(EXE): shell.o libsqlite3.a $(TCC) $(LDFLAGS) -o $@ shell.o libsqlite3.a $(LIBREADLINE) $(THREADLIB) $(LDLIBS)
去掉install,增加:
distclean: clean rm -f config.h
2. 拷贝Makefile.linux-gcc为Makefile,修改如下:
TCC = $(CROSS)gcc AR = $(CROSS)ar cr RANLIB = $(CROSS)ranlib #TCL_FLAGS = -I/home/drh/tcltk/8.4linux #LIBTCL = /home/drh/tcltk/8.4linux/libtcl8.4g.a -lm -ldl
编译:
在make menuconfig的user application部分可以看到刚添加的Database –>菜单,进入并选择SQLite,保存退出后按原先的步骤重新编译内核即可。如果只需要sqlite的库,那么make user_only就可以了。
编译完成后会在user/sqlite目录下生成库libsqlite3.a。
测试一下:
编写一个测试程序sqlitetest.c,代码如下(来自官方quick start):
#include #include static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0; i 然后为它写一个Makefile,大致如下: UCLINUX_PATH = /home/uClinux-dist SQLITE_PATH = $(UCLINUX_PATH)/user/sqlite CROSS = arm-elf- CPU_CFLAGS = -O3 -Wall -mapcs-32 -mtune=arm7tdmi -fno-builtin -msoft-float -Os -D__uClinux__ -D__ARM_CPU__ -I$(UCLINUX_PATH)/lib/uClibc/include -I$(UCLINUX_PATH)/linux-2.4.x/include -I$(SQLITE_PATH) -D_DEBUG_ CPU_LDFLAGS = -nostartfiles -Wl, -elf2flt -L$(UCLINUX_PATH)/lib/uClibc/lib CPU_ARFLAGS = r CPU_LDLIBS = $(UCLINUX_PATH)/lib/uClibc/lib/crt0.o $(UCLINUX_PATH)/lib/uClibc/lib/crti.o $(UCLINUX_PATH)/lib/uClibc/lib/crtn.o -lc MY_LDFLAGS = -L$(SQLITE_PATH) MY_LDLIBS = -lsqlite3 CFLAGS = $(CPU_CFLAGS) LDFLAGS = $(CPU_LDFLAGS) $(MY_LDFLAGS) LDLIBS = $(CPU_LDLIBS) $(MY_LDLIBS) TOPDIR = ./ CC = $(CROSS)gcc EXEC = sqlitetest CSRC = sqlitetest.c OBJS = $(patsubst %.c,%.o, $(CSRC)) all: $(EXEC) $(OBJS): %.o : %.c $(CC) $(CFLAGS) -c $ 运行make编译测试程序,生成的程序大小约300KB: linux:/home/work/sqlite # ll 总用量 688 drwxr-xr-x 2 root root 264 2006-10-09 11:25 . drwxr-xr-x 4 root root 160 2006-10-09 11:21 .. -rw------- 1 root root 982 2006-10-09 11:25 Makefile -rwxr--r-- 1 root root 315584 2006-10-09 11:25 sqlitetest -rw------- 1 root root 788 2006-10-09 11:21 sqlitetest.c -rwxr-xr-x 1 root root 396538 2006-10-09 11:25 sqlitetest.gdb -rw-r--r-- 1 root root 1600 2006-10-09 11:25 sqlitetest.o
接着将测试程序下载到目标板,测试运行结果如下:
# /home/sqlitetest /home/testdb.db "CREATE TABLE my_table(id int, name varchar(20))" # /home/sqlitetest /home/testdb.db "INSERT INTO my_table values(1, 'jianglj')" # /home/sqlitetest /home/testdb.db "INSERT INTO my_table values(2, 'Hily Jiang')" # /home/sqlitetest /home/testdb.db "SELECT * FROM my_table" id = 1 name = jianglj id = 2 name = Hily Jiang