1 sqlite3源码下载
https://www.sqlite.org/download.html
sqlite-autoconf-3250200.tar.gz
2 sqlite3源码编译
$ tar -zxvf sqlite-autoconf-3250200.tar.gz
$ cd sqlite-autoconf-3250200
$ touch build.sh
内容如下:
#!/bin/sh
./configure --host=arm-none-linux-gnueabi \
--disable-static \
--prefix=/usr/sqlite3arm ##/usr/sqlite3arm为预先设定好的目录,用于保存make和make install生成的文件
make && make install
$ chmod 755 build.sh && ./build.sh
3 sqlite3移植
拷贝/usr/sqlite3arm/bin/sqlite3到开发板的/usr/bin目录下
拷贝/usr/sqlite3arm/lib/*.so*到开发板的/usr/lib目录下
说明:如上步骤在制作根文件系统时候就应该完成了,我由于挂载了nfs,是直接copy的
4 sqlite3测试
4.1 命令行测试
相关命令行:
// 帮助
.help
// 建表
CREATE TABLE "stu_tb" ("id" INTEGER PRIMARY KEY NOT NULL,"name" TEXT NOT NULL,"phone" INTEGER NOT NULL,"gid" INTEGER NOT NULL);
// 查表
SELECT * FROM stu_tb
// 插入信息
INSERT INTO stu_tb VALUES(0002,'simon',12345678901,1);
...
// 退出
.exit
4.2 C语言调用测试
测试代码参考:
#include <stdio.h>
#include <sqlite3.h>
static int callback (void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for (i = 0; i < argc; i++) {
printf ("%s = %s \n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf ("\n");
return 0;
}
int main (int argc, char **argv)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if (argc != 3) {
fprintf (stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
return 1;
}
/* 打开数据库 */
rc = sqlite3_open (argv[1], &db);
if (rc) {
fprintf (stderr, "Can't open database:%s\n", sqlite3_errmsg(db));
return 1;
}
rc = sqlite3_exec (db, argv[2], callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf (stderr, "SQL error:%s\n", zErrMsg);
sqlite3_free (zErrMsg);
}
/* 关闭数据库 */
sqlite3_close (db); return 0;
}
测试代码编译:
$ arm-none-linux-gnueabi-gcc mySqlite3.c -lsqlite3 -L/usr/sqlite3arm/lib -I/usr/sqlite3arm/include -o sqlarm
## arm-none-linux-gnueabi-gcc 自己设定的交叉编译器
## /usr/sqlite3arm/lib:交叉编译sqlite3时的路径参考文章开头
## /usr/sqlite3arm/include:交叉编译sqlite3时的路径参考文章开头