当前位置:网站首页>嵌入式Linux中使用SQLite3数据库
嵌入式Linux中使用SQLite3数据库
2022-06-10 11:44:00 【Silent starry sky】
嵌入式Linux中使用SQLite3数据库
SQLite3是一款轻量级的文件型数据库,非常适合嵌入式环境下使用,最近搞的Linux嵌入式项目中需要用到这款数据库,就研究了一下怎么在嵌入式Linux中使用,记录下来以备查用。
我的嵌入式核心板MPU用的是TI的AM3352这款A8单核处理器,交叉编译环境安装在虚拟机的国产Linux系统Deepin中,顺便吐槽一下,大部分网上教程都是用ubuntu来建立交叉编译环境,做嵌入式开发用个桌面版就足够,建议大家不妨用用Deepin,更符合国人的使用习惯,很多软件都自带不需要重新安装。用了一段时间感觉还不错。交叉编译工具用的是GNU的arm–none-linux-gnueabi这款交叉编译工具。怎么在Ubuntu或DeepIn中搭建32位arm的交叉编译环境就不在这赘述了,网上有很多文章。
1.首先去SQLite官网下载最新源码,如下图: 
注意要下载带编译配置工具的这个。
2.拷贝到虚拟机的Deepin系统中,解压缩到用户目录中,在桌面直接解压缩或用终端命令解压缩也行,解压缩命令:tar -zxvf sqlite-autoconf-3380500.tar.gz。
3.进入解压目录,打开终端,如下图:
运行编译配置命令:./configure --host=arm-none-linux-gnueabi --prefix=/home/xxx/dist-arm-sqlite3
这里要注意的是配置命令有两个参数:
–host 这个参数是将编译工具配置为本虚拟机上安装的交叉编译工具,注意”arm-none-linux-gnueabi”这是交叉编译工具的前缀名,完整的gcc交叉编译工具名是:“arm-none-linux-gnueabi-gcc”。
–prefix 这个参数是配置编译完成后安装时将SQLite3的编译生成文件安装到的绝对目录的路径。这个自己定义一个有写入权限的目录即可。
配置过程如下图:
4.最后运行编译和安装命令:make && make install
等待一段时间等编译和安装完成后打开前面设置的目录,里面就是编译安装后的结果,如下图:
其中“bin”目录里的sqlite3文件是在嵌入式Linux中运行的sqlite3的命令行工具,“include"目录下是C/C++开发用的头文件,”lib"目录是sqlite3的静态库和动态库文件。所以我们开发要用的就是“include”目录和"lib"目录。
这样sqlite3在嵌入式Linux中开发需要的文件就都有了,可以用程序测试一下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "sqlite3.h"
char DBFileName[80];
char DBTableName[20];
int main( int argc,char* argv[] )
{
char SqlStr[256];
char* ErrMsg;
char str[64];
int startidx;
int i1, rc;
time_t t;
struct tm *p;
sqlite3* db;
startidx = 0;
strcpy( DBFileName, "/mnt/nandflash/test.db");
strcpy( DBTableName, "table1");
//创建数据库文件
rc = sqlite3_open( DBFileName, &db );
sprintf( SqlStr, "create table %s(Name char(50), Value int, Date char(50))",
DBTableName );
//创建表
sqlite3_exec( db, SqlStr, 0, 0, &ErrMsg );
//往表里插入10条记录
for( i1=startidx; i1<startidx+10; i1++ )
{
time( &t );
p = localtime ( &t );
sprintf( str, "%d.%d.%d-%d:%d:%d\n", p->tm_year + 1900, p->tm_mon+1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec );
printf("%d\n", i1+1);
sprintf(SqlStr, "insert into %s values(\"Name%d\", %d, \"%s\")",
DBTableName, i1+1, i1+1, str );
sqlite3_exec( db, SqlStr, 0, 0, &ErrMsg );
sleep(1);
}
sqlite3_close( db );
return 0;
}
码字不易,如果本文对您有用请随手点个赞,谢谢!^_^
边栏推荐
- mTabLayout.setOnTabSelectedListener is deprecated
- QA of some high frequency problems in oauth2 learning
- Pytorch tensor high order operation
- How zoom closes the microphone when joining a meeting
- Dell G7 computer shutdown keypad
- 数据在内存中的存储方式
- 87. (leaflet house) leaflet military plotting - straight arrow modification
- LVS+Keepalived高可用群集
- IDEA自定义配置链接nacos
- web开发发展趋势,web计划开发
猜你喜欢

线性代数的本质4 矩阵乘法与线性复合变换

入门篇:从 etcd 名字的由来,到安装部署和使用姿势

《浅谈数组》

还活在上个时代,Etcd 3.0 实现分布式锁竟如此简单!

Transfomer components and pytoch

2022 年 5 月产品大事记

Amateurs don't ask for help, drag and drop for 30 seconds to make the cover image

数据在内存中的存储方式

海量数据:华为高斯数据库第一股(深度)

【 ten thousand people single wooden bridge 】 how to arrange life in that summer after the college entrance examination?
随机推荐
并发bug之源(一)-可见性
“职” 为等你!| 图书策划编辑(会议论文集方向)
如何编写产品营销策划方案
如何写出不容易内存溢出的代码
Privilege application permission configuration
shell大全
Flutter socketio example
多线程杀手锏---countDownLatch&&CyclicBarrier
[PaperNote] Confidential Computing Direction
Clip usage
Tmux使用
交出 Firehouse 数据库访问权:推特准备满足马斯克要求
[WIP] Openstack Masakari (by quqi99)
Dell G7 computer shutdown keypad
Testing ovn manually based on LXD (by quqi99)
现场勘察制度
mTabLayout.setOnTabSelectedListener is deprecated
Living in the last era, etcd 3.0 is so simple to implement distributed locks!
How zoom closes the microphone when joining a meeting
浅谈分布式事务