十 . 树莓派A20 sqlite数据库移植

1 简介

1.1 功能特性

SQLite是一款轻型的嵌入式数据库。在嵌入式设备中,可能只需要几百K的内存就够了。它的处理速度比Mysql、PostgreSQL这两款著名的数据库都还快。
SQLite属于关系型数据库。

常用关系型数据库:

PC端:Oracle、MySQL、SQL Server、Access、DB2、Sybase
嵌入式\移动客户端:SQLite
这种数据库存储数据是以表(table)为基本单位的。

1.2 参考资料

1.博客链接:链接
2.具体到每一个操作方法的博客:链接
3.使用C操作SQLite的博客:链接
4.sqlite_master介绍:链接

2 移植

2.1 下载源码

sqlite的官方网址为:链接
sqlite下载网址为:链接

我下载的源码如下所示:

《十 . 树莓派A20 sqlite数据库移植》 源码下载

完成解压:

$ tar xzf sqlite-autoconf-3200000.tar.gz

2.2 编译安装

2.2.1 安装到本机

可以执行如下命令:

$ cd sqlite-autoconf-3200000/
$ ./configure --prefix=/home/wityuan/Downloads/sqlite-autoconf-3200000/temp CC=gcc
$ make
$ sudo make install

之后,安装成功,到temp目录下,查看文件:

《十 . 树莓派A20 sqlite数据库移植》 安装成功
《十 . 树莓派A20 sqlite数据库移植》 运行sqlite命令

2.2.2 安装到树莓派A20上

这种交叉编译与安装到PC上步骤是差不多的。

$ make clean
$ cd sqlite-autoconf-3200000/
$ ./configure --prefix=/home/wityuan/Downloads/sqlite-autoconf-3200000/temp_A20 --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc LD=arm-linux-gnueabihf-ld
$ make
$ sudo make install

将生成的temp_A20目录下的文件内容对应拷贝到/usr/lib和/usr/include目录下。

需要注意,前面做了PC端的编译,如果不make clean,那么后面make的时候,使用的还是以前的make的结果。

最终我们生成了如下内容:

《十 . 树莓派A20 sqlite数据库移植》 生成sqlite相关文件

如果在A20上运行不起来,我们首先要看sqlite3的文件格式,如下图所示的错误格式:

《十 . 树莓派A20 sqlite数据库移植》 错误格式
《十 . 树莓派A20 sqlite数据库移植》 正确格式

下面可以验证:
先将 bin目录下的sqlite3移到A20的bin目录下,然后执行sqlite3。

root@marsboard:~# chmod +x sqlite3
root@marsboard:~# cp sqlite3 /bin/
root@marsboard:~# sqlite3
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>
sqlite>

可以发现,已经运行起来了。

下面还有一个比较重要的步骤:

上面只是在A20板子上能运行应用程序,但是在写程序编码的时候,我们还是需要包含一些库等等,才能让PC机去执行这个过程,所以,需要将交叉编译过后的lib,include等文件拷贝到PC机上的交叉编译的工具的相应目录下。

先看PC机上的交叉编译目录位置:

《十 . 树莓派A20 sqlite数据库移植》 PC机交叉编译工具位置

我使用的是arm-linux-gnueabihf工具链。所以我需要操作的内容如下:

wityuan@ubuntu:~/Downloads/sqlite-autoconf-3200000/temp_A20$ sudo cp include/* /usr/arm-linux-gnueabihf/include/
wityuan@ubuntu:~/Downloads/sqlite-autoconf-3200000/temp_A20$ sudo cp lib/* /usr/arm-linux-gnueabihf/lib/
cp: omitting directory `lib/pkgconfig'
wityuan@ubuntu:~/Downloads/sqlite-autoconf-3200000/temp_A20$ 

《十 . 树莓派A20 sqlite数据库移植》 拷贝库和头文件

3 使用SQLite数据库

3.1 基本操作思想

数据库存储数据的步骤:
1)新建一张表(table)
2)添加多个字段(column,列,属性)
3)添加多行记录(row.record,每行存放多个字段对应的值)

3.2 基本操作方法

3.2.1 数据库操作方法分类

1.数据定义语句(DDL:Data Definition Language)
包括create和drop等操作,在数据库中创建新表或删除表(create table或 drop table)。

2.数据操作语句(DML:Data Manipulation Language)
包括insert、update、delete等操作,也就是添加、修改、删除表中的数据。

3.数据查询语句(DQL:Data Query Language)
可以用于查询获得表中的数据,select是用得比较多的语句,还有一些常用的语句,例如:where,order by,group by和having。

3.2.2 开始操作

1.创建数据库

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> .schema
sqlite> 

通过该命令,就能生成一个数据库test.db

2.创建表

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> CREATE TABLE stutable(sid,sname,sage)
   ...> ;
sqlite> .schema
CREATE TABLE stutable(sid,sname,sage);
sqlite> 
sqlite> 

这里就可以创建出表stutable。

3.插入数据

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE stutable(sid,sname,sage);
sqlite> insert into stutable(sid,sname,sage) values(1,"jack","23")
   ...> ;
sqlite> insert into stutable(sid,sname,sage) values(2,"rose","21");
sqlite> 

这里表示插入两条信息,1,jack,23与2,rose,21两条。

4.查询数据

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> select * from stutable
   ...> ;
1|jack|23
2|rose|21
sqlite> 

这里表示的就是查询到的上面插入的2条数据。

5.修改数据

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> update stutable set sname="answer" where sid=1 ;
sqlite> select * from stutable
   ...> ;
1|answer|23
2|rose|21
sqlite> 

6.一些命令行方式小技巧
1)头部分隔

sqlite> .header on
sqlite> select * from stutable;
sid         sname       sage      
----------  ----------  ----------
1           answer      23        
2           rose        21        
sqlite> 

7.删除表内容

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> 
sqlite> select * from stutable
   ...> ;
1|answer|23
2|rose|21
sqlite> delete from stutable;
sqlite> select * from stutable;
sqlite> 

可以看到最后查询表的内容时候,已经不存在了。

8.删除其中一项内容

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> .table
stutable
sqlite> select * from stutable
   ...> ;
1|john|23
2|rose|22
sqlite> 
sqlite> delete from stutable where sid=1;
sqlite> select * from stutable;
2|rose|22
sqlite> 

9.删除表

wityuan@ubuntu:~/Desktop$ sudo sqlite3 test.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> .table
stutable
sqlite> drop table stutable;
sqlite> .table
sqlite> 

可以看到,最后使用.table后,stutable表已经不存在了。

4 使用C语言来操作SQLite

4.1 创建数据库

建立create_database.c文件,代码内容如下:

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

int main(int argc,char *argv[])
{
  int ret;
  sqlite3 *db = 0;
  ret = sqlite3_open("./sqlite3-demo.db",&db);
  if(ret != SQLITE_OK){
    fprintf(stderr,"Cannot open db: %s\n",sqlite3_errmsg(db));
    return 1;
  }
  printf("Open database\n");
  return 0;
}

由于我是使用的官方源码编译到ubuntu中的,所以要注意将lib和include文件拷贝到系统的目录中,如下所示:

$ sudo cp include/* /usr/include/
$ sudo cp lib/* /usr/lib/ 
cp: omitting directory `lib/pkgconfig'

然后编译,运行:

$ gcc create_database.c -o create_database -lsqlite3
$ ./create_database 
Open database
$  ls 
create_database  create_database.c  sqlite3-demo.db

可以看到,创建数据库sqlite3-demo.db成功。

4.2 创建表

新建一个c文件,命名为create_table.c,内容如下:

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

int main(int argc,char *argv[])
{
  int ret;
  sqlite3 *db = 0;
  char *ErrMsg;
  char *sql_create_table = "create table stutable(sid,sname,sage);";
  ret = sqlite3_open("./sqlite3-demo.db",&db);
  if(ret != SQLITE_OK){
    fprintf(stderr,"Cannot open db: %s\n",sqlite3_errmsg(db));
    return 1;
  }
  printf("Open database\n");

  ret = sqlite3_exec(db,sql_create_table,0,0,&ErrMsg);
  if(ret != SQLITE_OK)
  {
    fprintf(stderr,"SQL Error:%s\n",ErrMsg);
    sqlite3_free(ErrMsg);
  }

  return 0;
}

然后,编译执行:

wityuan@ubuntu:~/Desktop/sqlite$ gcc create_table.c -o create_table -lsqlite3
wityuan@ubuntu:~/Desktop/sqlite$ sqlite3 sqlite3-demo.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> .table
sqlite> .exit
wityuan@ubuntu:~/Desktop/sqlite$ ./create_table 
Open database
wityuan@ubuntu:~/Desktop/sqlite$ 
wityuan@ubuntu:~/Desktop/sqlite$ sqlite3 sqlite3-demo.db
SQLite version 3.20.0 2017-08-01 13:24:15
Enter ".help" for usage hints.
sqlite> .table
stutable
sqlite> .exit
wityuan@ubuntu:~/Desktop/sqlite$ 

可以看到,成功的创建表stutable。

再来看这么一个问题,也就是上面的这个程序执行两次,就会发现程序会报错:

《十 . 树莓派A20 sqlite数据库移植》 创建数据库报错

显然,解决思路是判断表存在与否,如果不存在,我们就创建,否则就不再创建了。

写到这里,就需要知道sqlite3本身也会维护另外一张表,表名称为sqlite3_master;其存储了我们建立的表的信息,如:表名称,创建的sql语句等。如下命令所示:

sqlite> select * from sqlite_master;
type|name|tbl_name|rootpage|sql
table|stutable|stutable|2|CREATE TABLE stutable(sid,sname,sage)
sqlite> 

可以看到sqlite_master,该表创建的字段有type,name,tbl_name,rootpage,sql。

所以在程序里面,解决起来,也很简单,直接查询sqlite_master中含有stutable记录的数量,如果为0,就说明没有创建该表,就可以执行创建操作。否则,就不用创建了。

使用的语句为:

select count(*) from sqlite_master where type='table' and name ='stutable';  

修改上面的代码,使用如下代码:

#include "sqlite3.h"
#include "stdio.h"
#include "stdlib.h"
int table_exist = 0;
static int callback(void *NotUsed,int argc,char **argv,char **azColName){
  int i;
  printf("argc:%d\r\n",argc);
  for(i = 0 ;i < argc; i ++){
    printf("%s = %s\n", azColName[i],argv[i]?argv[i]:"NULL");
  }
  table_exist = atoi(argv[0]);
  printf("table_exist:%d\r\n",table_exist);
  printf("\n");
  return 0;
}
int main(int argc,char *argv[])
{
  int ret;
  sqlite3 *db = 0;
  char *ErrMsg;
  char *sql_create_table = "create table stutable(sid,sname,sage);";
  char *sql_table_exits = "select count(*) from sqlite_master where type='table' and name ='stutable'";
  ret = sqlite3_open("./sqlite3-demo.db",&db);
  if(ret != SQLITE_OK){
    fprintf(stderr,"Cannot open db: %s\n",sqlite3_errmsg(db));
    return 1;
  }
  printf("Open database\n");
  ret = sqlite3_exec(db,sql_table_exits,callback,0,&ErrMsg);
  if(ret == SQLITE_OK)
  {
    if(table_exist == 0)
    {
      ret = sqlite3_exec(db,sql_create_table,0,0,&ErrMsg);

      if(ret != SQLITE_OK)
      {
        fprintf(stderr,"SQL Error:%s\n",ErrMsg);
        sqlite3_free(ErrMsg);
      }
      printf("---create table----\r\n");
    }
    else
    {
      printf("--table exists---\r\n");
    }
  }
  else
  {
    fprintf(stderr,"SQL Error:%s\n",ErrMsg);
  }
  return 0;
}

我再列出执行结果:

wityuan@ubuntu:~/Desktop/sqlite$ ./create_table
Open database
argc:1
count(*) = 0
table_exist:0

---create table----
wityuan@ubuntu:~/Desktop/sqlite$ ./create_table
Open database
argc:1
count(*) = 1
table_exist:1

--table exists---
wityuan@ubuntu:~/Desktop/sqlite$ 

可如同所示:

《十 . 树莓派A20 sqlite数据库移植》 sqlite创建表

4.3 插入数据

该操作也是需要判断是否已经存在该条目,如果有,则使用update更新数据,否则就执行insert操作。
该操作也是使用count(*),思想与上面的一样,所以也就不再往下面说了。

    原文作者:wit_yuan
    原文地址: https://www.jianshu.com/p/33c923e1a821
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞