嵌入式Linux sqlite3移植与使用

概述

sqlite3是一个小型数据库,代码量少,运行占内存也比较少,采用C 编写,所以天生适合嵌入式系统中,尤其是嵌入式linux,相当支持,sqlite3可以直接通过shell运行,不过这个也只限于测试使用,在实际的项目编程中,我们还是要使用sqlite3提供的C/C++接口函数,也就是API接口。

sqlite3 移植

sqlite官网下载C源码:https://www.sqlite.org/download.html
在这里插入图片描述

解压压缩包:tar -zxvf sqlite-autoconf-3360000.tar.gz
运行配置文件:./configure --host=arm-linux --prefix=/work/src_packages/sqlite-autoconf-3071401/dist-sqlite3
–host:指定交叉编译工具, 和编译 Linux 的编译器一致
–prefix: 指定安装目录, 编译后生成的文件放在此目录, 必须是绝对路径
编译:执行make,再执行make install
编译结束过后,在源码目录下生成dist-sqlite3文件夹
在这里插入图片描述

将/dist-sqlite3/bin/sqlite3拷贝到开发板的/bin目录下,并执行chmod 777 sqlite3。
将/dist-sqlite3/lib/所有文件拷贝到开发板的/lib目录下。
终端输入”sqlite3”,进入sqlite3。

sqlite3 常用命令

进入sqlite3命令终端:#sqlite3
获取点命令清单:sqlite>.help
退出sqlite3操作:sqlite>.exit 或sqlite>.quit
打开或创建数据库:sqlite>.open test.db
查看数据库文件信息:sqlite>.database
创建表:CREATE TABLE语句;
查看所有表:sqlite>.tables
查看所有表的完整信息:sqlite>.schema
删除表:DROP TABLE语句
某个表中添加新的数据行:INSERT INTO语句
数据库表中获取数据:SELECT语句
列出当前显示格式的配置:sqlite>.show
设置显示格式之打开表头:sqlite>.header on
设置显示格式之列款:sqlite>.width 2
示例:

# sqlite3 
SQLite version 3.36.0 2021-06-18 18:36:39
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open test.db
sqlite> .databases
main: /backup0/test.db r/w
sqlite> create table demo (id int,name char,age int);
sqlite> .tables                                  
demo
sqlite> .schema
CREATE TABLE demo (id int,name char,age int);
sqlite> insert into demo values(100,'WangWu',23);
sqlite> insert into demo values(101,'Tommm',25);
sqlite> .header on
sqlite> .mode column
sqlite> .show
        echo: off
         eqp: off
     explain: auto
     headers: on
        mode: column
   nullvalue: ""
      output: stdout
colseparator: "|"
rowseparator: "\n"
       stats: off
       width: 
    filename: test.db
sqlite> select * from demo;
id   name    age
---  ------  ---
100  WangWu  23 
101  Tommm   25 
sqlite> .exit
#

sqlite3 API使用

打开数据库

int sqlite3_open( 文件名, sqlite3 ** );

关闭数据库

int sqlite3_close(sqlite3 *);

执行sql语句

int sqlite3_exec(sqlite3*,const char sql,sqlite3_callback,void,char **errmsg );

查询数据库

int sqlite3_get_table(sqlite3*,const char *sql, char ***resultp, int *nrow, int *ncolumn, char **errmsg);

sqlite3 编程实例

实现思路:

  • 打开数据库,执行sqlite3_open;
  • 创建数据库表格,利用sqlite3_exec执行create table语句;
  • 根据输入指令,执行插入、删除、查询、退出操作;
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#include <strings.h>

#define DATABASE "fru.db"
typedef struct{
	char time[20];
	char name[16];
	float cost;//成本
	float price;//售价
	char in_out[5];//进货in,出货out
	float number;//交易量
	float money;//交易金额(成本或售价*交易量)
	float total;//库存总量
}fruit_t;

void operate_table_cmd_menu()
{
	puts("/******************************************/");
	puts("*operate table cmd menu:");
	printf("*1:insert 2:delete 3:query 4:update 5:quite \n");
	puts("/******************************************/");
	printf("Please input cmd:");
}

const char *get_local_time(char *time_str);
fruit_t *get_last_row(sqlite3 *db,fruit_t *fruit);
int query_callback(void *para, int f_num, char **f_value, char **f_name);
int do_insert_sqlite3(sqlite3 * db);
int do_delete_sqlite3(sqlite3 * db);
int do_query_sqlite3(sqlite3 * db);
void get_columns_name(sqlite3 * db);//获取字段名称
int do_update_sqlite3(sqlite3 * db);

int main(int argc, const char *argv[])
{
	sqlite3 * db;
	char *errmsg;
	int cmd;
	char sql[128]={};

	//打开sqlite3数据库
	if(sqlite3_open(DATABASE,&db) != SQLITE_OK)
	{
		printf("%s\n",sqlite3_errmsg(db));
		exit(-1);
	}
	else
	{
		printf("open %s success.\n",DATABASE);
	}

	//创建一张数据库的表格   将ID设为主键值,并设为自增字段(保证字段唯一性)
	sprintf(sql,"create table fru(time char,name char,cost Integer,price Integer,in_out char,number Integer,money Integer,total Integer);");
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
	}
	else
	{
		printf("create or open table success.\n");
	}

	while(1)
	{
		operate_table_cmd_menu();
		while(scanf("%d",&cmd) == 0)
		{
			getchar();
			operate_table_cmd_menu();
		}
		getchar();

		switch(cmd)
		{
			case 1:
				do_insert_sqlite3(db);
				break;
			case 2:
				do_delete_sqlite3(db);
				break;
			case 3:
				do_query_sqlite3(db);
				break;
			case 4:
				do_update_sqlite3(db);
				break;
			case 5:
				sqlite3_close(db);
				exit(0);
			default:
				printf("Error cmd.\n");
		}
	
	}
	return 0;
}

const char *get_local_time(char *time_str)
{
	time_t tim_t;
	struct tm loc_t;

	time(&tim_t);

	loc_t = *localtime(&tim_t);

	sprintf(time_str,"%04d-%d-%02d %02d:%02d:%02d",loc_t.tm_year+1900,loc_t.tm_mon+1,loc_t.tm_mday,loc_t.tm_hour,loc_t.tm_min,loc_t.tm_sec);

	return time_str;
}

fruit_t *get_last_row(sqlite3 *db,fruit_t *fruit)
{
	char **resultp;
	int nrow;
	int ncolumn;
	char sql[128];
	int index_base;
	char *errmsg;

	sprintf(sql,"select * from fru where name='%s';",fruit->name);

	sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg);
	
	if(nrow == 0)
	{
		return NULL;
	}
	else
	{
		index_base = (nrow)*(ncolumn);
		fruit->cost = atof(resultp[index_base+2]);
		fruit->price = atof(resultp[index_base+3]);
		fruit->total = atof(resultp[index_base+7]);
	}
	return fruit;
}
int do_insert_sqlite3(sqlite3 * db)
{
	fruit_t fruit;
	char sql[128];
	char *errmsg;
	char type[10];

	bzero(&fruit,sizeof(fruit));

	get_local_time(fruit.time);

	printf("input fruit name:");
	scanf("%s",fruit.name);
	getchar();


	if(get_last_row(db,&fruit) != NULL)//系统中已经有这种水果的记录
	{
		puts("*************************************");	
		printf("%s cost:%.2f price:%.2f\n",fruit.name,fruit.cost,fruit.price);
		puts("*************************************");	
		printf("if you change it[y|n]:");

		scanf("%s",type);
		getchar();

		if(strncasecmp(type,"y",strlen("y")) == 0)
		{
		char value[20];
			printf("change cost:%.2f of %s [cost|n]:",fruit.cost,fruit.name);
			scanf("%s",value);//输入成本修改成本,输入n放弃修改
			getchar();
			if(strncasecmp(value,"n",strlen("n")) != 0)
				fruit.cost = atof(value);
		
			printf("change price:%.2f of %s [price|n]:",fruit.price,fruit.name);
			scanf("%s",value);//输入单价修单价,输入n放弃修改
			getchar();
			if(strncasecmp(value,"n",strlen("n")) != 0)
				fruit.price = atof(value);
		}
		printf("Please input the number of %s:",fruit.name);
		scanf("%f",&fruit.number);
		getchar();

		fruit.total = fruit.total +fruit.number;
	}
	else//系统中没有的水果种类
	{
		printf("Please input the cost of %s [cost]:",fruit.name);
		while(scanf("%f",&fruit.cost) == 0)
		{
			getchar();
			printf("Please input the cost of %s [cost]:",fruit.name);
		}
		getchar();
	
		printf("Please input the price of %s [price]:",fruit.name);
		while(scanf("%f",&fruit.price) == 0)
		{
			getchar();
			printf("Please input the price of %s [price]:",fruit.name);
		}
		getchar();
			
		printf("Please input the number of %s:",fruit.name);
		scanf("%f",&fruit.number);
		getchar();

		fruit.total = fruit.number;
	}
	
	sprintf(fruit.in_out,"in");

	fruit.money = fruit.cost * fruit.number;


	sprintf(sql,"insert into fru values('%s','%s',%.4f,%.4f,'%s',%.4f,%.4f,%.4f);",fruit.time,fruit.name,fruit.cost,fruit.price,fruit.in_out,fruit.number,fruit.money,fruit.total);//自增字段的值传NULL

	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK )
	{
		printf("%s\n",errmsg);
		return -1;
	}
	else
	{
		printf("insert:\n ");

		sprintf(sql,"select * from fru where time='%s';",fruit.time);
		get_columns_name(db);//获取字段名称
		sqlite3_exec(db,sql,query_callback,NULL,&errmsg);
		printf("done\n");

		return 0;
	}
}
int query_callback(void *para, int f_num, char **f_value, char **f_name)
{

	int i;
	for(i=0;i<f_num;i++)
	{
		if(i == 0)
			printf("\t%-19s ",f_value[i]);
		else
			printf("%-10s ",f_value[i]);
	}
	puts("");
	return 0;
}
void get_columns_name(sqlite3 * db)
{
	char **resultp;
	int nrow;
	int ncolumn;
	char *errmsg;
	int i;

	sqlite3_get_table(db,"select * from fru",&resultp,&nrow,&ncolumn,&errmsg);
	printf("\t%-19s ",resultp[0]);
	for(i=1;i<ncolumn;i++)
	{
		printf("%-10s ",resultp[i]);
	}
	puts("");
}
int do_delete_sqlite3(sqlite3 * db)
{
	fruit_t fruit;
	char sql[128];
	char *errmsg;
	char value[20];

	bzero(&fruit,sizeof(fruit));

	get_local_time(fruit.time);

	printf("input fruit name:");
	scanf("%s",fruit.name);
	getchar();


	if(get_last_row(db,&fruit) != NULL)//系统中有这种水果
	{
		printf("input the out number of %s:",fruit.name);

		scanf("%s",value);
		getchar();

		if(atof(value) > fruit.total)//剩余的数量不够
		{
			printf("The total number:%.4f of %s is not enough, please stock it.\n",fruit.total,fruit.name);
			return -1;
		}
		else
		{
			fruit.number = atof(value);
			fruit.money = fruit.price * fruit.number;
			fruit.total = fruit.total - fruit.number;
		}
	}
	else//系统中没有这种水果
	{
		printf("We don't have this fruit:%s. Please stock it.",fruit.name);
		return -1;
	}
	
	sprintf(fruit.in_out,"out");

	printf("are you sure delete number:%.4f of %s[y|n]:",fruit.number,fruit.name);
	scanf("%s",value);
	getchar();

	if(strncasecmp(value,"y",strlen("y")) == 0)
	{
		sprintf(sql,"insert into fru values('%s','%s',%.4f,%.4f,'%s',%.4f,%.4f,%.4f);",fruit.time,fruit.name,fruit.cost,fruit.price,fruit.in_out,fruit.number,fruit.money,fruit.total);//自增字段的值传NULL

		if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK )
		{
			printf("%s\n",errmsg);
			return -1;
		}
		else
		{
			printf("delete done\n ");
			return 0;
		}
	}
	else
	{
		printf("delete cancelled.\n");
		return 0;
	}
}
int do_query_sqlite3(sqlite3 * db)
{
	fruit_t fruit;
	char sql[128];
	char *errmsg;
	char value[96];

	bzero(&fruit,sizeof(fruit));

	puts("*************************************");	
	printf("*Examples of query types:\n");
	puts("*[all | name='apple' | in_out='in']");
	//puts("*[all total | apple total | banana total]");
	puts("*[time<'2018-12-5 17:08:08' | total>90 and total<200]");
	puts("*[name='apple' and in_out='in']");
	puts("*************************************");	

	printf("Pease input query type:");
	scanf("%s",value);
	getchar();

	if(strncasecmp("all",value,strlen(value)) == 0)
		sprintf(sql,"select * from fru;");
	else
		sprintf(sql,"select * from fru where %s;",value);//自增字段的值传NULL

	get_columns_name(db);//获取字段名称
	if(sqlite3_exec(db,sql,query_callback,NULL,&errmsg) != SQLITE_OK )
	{
		printf("%s\n",errmsg);
		return -1;
	}
	else
	{
		printf("query done\n ");
		return 0;
	}
}
int do_update_sqlite3(sqlite3 * db)
{
	fruit_t fruit;
	char sql[128];
	char *errmsg;
	int type;

	bzero(&fruit,sizeof(fruit));

	get_local_time(fruit.time);

	printf("input fruit name:");
	scanf("%s",fruit.name);
	getchar();


	if(get_last_row(db,&fruit) != NULL)//系统中已经有这种水果的记录
	{
		puts("*************************************");	
		printf("%s cost:%.2f price:%.2f\n",fruit.name,fruit.cost,fruit.price);
		puts("*************************************");	
		printf("if you update it[1:yes 2:cancel]:");

		scanf("%d",&type);
		getchar();

		char value[20];
		if(type == 1)
		{
			printf("update cost:%.2f of %s [cost|n]:",fruit.cost,fruit.name);
			scanf("%s",value);//输入成本修改成本,输入n放弃修改
			getchar();
			if(strncasecmp(value,"n",strlen("n")) != 0)
				fruit.cost = atof(value);
			
			printf("update price:%.2f of %s [price|n]:",fruit.price,fruit.name);
			scanf("%s",value);//输入单价修单价,输入n放弃修改
			getchar();
			if(strncasecmp(value,"n",strlen("n")) != 0)
				fruit.price = atof(value);
		}
		else
		{
			printf("update cancelled\n");
			return 0;
		}
		sprintf(fruit.in_out,"update");

		sprintf(sql,"insert into fru values('%s','%s',%.4f,%.4f,'%s',%.4f,%.4f,%.4f);",fruit.time,fruit.name,fruit.cost,fruit.price,fruit.in_out,fruit.number,fruit.money,fruit.total);//自增字段的值传NULL
		if(sqlite3_exec(db,sql,query_callback,NULL,&errmsg) != SQLITE_OK )
		{
			printf("%s\n",errmsg);
			return -1;
		}
		else
		{
			printf("update:\n");
			get_columns_name(db);//获取字段名称
			sprintf(sql,"select * from fru where time='%s';",fruit.time);
			sqlite3_exec(db,sql,query_callback,NULL,&errmsg);
			printf("update done\n ");
			return 0;
		}
	}
	else//系统中没有的水果种类
	{
		printf("We don't have this fruit:%s. Please stock it.",fruit.name);
	}
		return 0;
}

sqlite3 测试验证

交叉编译:
arm-linux-gcc -I /home/ubuntu/sqlite3/install/include/ -L /home/ubuntu/sqlite3/install/lib/ -o sql testSql.c -lsqlite3 –ldl
-I 指定了 sqlite3.h 所在路径,
-L 指定 sqlite3 的 lib 库路径,即libsqlite3.a所在目录。
-o 编译生成的文件名为sql ,testSql.c是源程序文件;
-lsqlite3指明要链接静态库文件libsqlite3.a。
将生成的可执行文件拷贝到开发板,运行测试:
在这里插入图片描述

友情链接,sqlite3菜鸟教程:https://www.runoob.com/sqlite/sqlite-create-table.html
友情链接,sqlite3 C代码示例:https://blog.csdn.net/m0_37542524/article/details/84842154
友情链接,sqlite3 基础概念:https://www.cnblogs.com/xishuichangliu/p/6270291.html

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 语言中是如何被使用的? SQLite是一种轻量级的关系型数据库管理系统,它被广泛地用于各种平台上,包括C语言。在C语言中,SQLite提供了简单易用的API来访问数据库SQLite API由大约40个函数组成,可以在程序中执行各种各样的数据库操作。 在C语言中,使用SQLite是非常方便的。只需要下载SQLite库文件,然后在程序中包含相关的头文件,就可以开始使用SQLite API。SQLite API包括创建数据库、查询数据、修改数据等许多功能。其中最常用的函数包括 sqlite3_open()、sqlite3_exec()、sqlite3_prepare_v2()、sqlite3_step()、sqlite3_finalize() 等。 使用SQLite API的第一步是打开一个数据库,可以使用 sqlite3_open() 函数来打开一个SQLite数据库文件。一旦数据库被打开,就可以使用 SQLite SQL 来执行各种数据库操作,如创建表、插入数据、查询数据等。这些操作可以使用 sqlite3_exec() 函数来执行。 对于复杂的操作,可以使用 sqlite3_prepare_v2() 和 sqlite3_step() 函数来执行。sqlite3_prepare_v2() 函数用于准备 SQL 语句,而 sqlite3_step() 函数用于执行 SQL 语句并获取结果。最后,可以使用 sqlite3_finalize() 函数来释放资源。 总之,SQLite是一种非常强大而又易于使用数据库管理系统,在C语言中可以很方便地使用它来处理数据。它不仅可以用于开发桌面应用程序,也可以用于移动应用程序和嵌入式系统。 ### 回答2: 语言中的用途是什么? SQLite是一种轻量级的关系型数据库管理系统,它可以嵌入到C语言中,并被广泛用于各种应用程序的数据存储和管理。SQLite的独立性和可移植性使得它成为最受欢迎的嵌入式数据库之一。 在C语言中,使用SQLite可以快速、高效地管理大量数据,包括存储、查询、更新和删除等操作。SQLite还提供了各种功能强大的API,使得开发人员可以轻松地与数据库进行交互,以实现各种复杂的数据操作,例如事务处理、查询优化和数据加密等。 由于SQLite使用纯C语言编写,因此它可以在几乎所有支持C语言的平台上运行,包括Windows、Linux、Unix和Mac OS X等操作系统。此外,SQLite还提供了多语言支持,使得它可以在不同的编程语言中使用,例如Python、Java和PHP等。 总之,SQLite在C语言中的用途非常广泛,它为开发人员提供了一种高效、可靠且易于使用数据库管理系统,极大地简化了应用程序的数据处理和管理。 ### 回答3: 语言中的作用是什么? SQLite是一种轻量级的关系型数据库管理系统,是一款由C语言编程实现的程序库,无需独立的服务器进程或系统,而是直接读取或写入磁盘上存储的文件。在C语言中,SQLite扮演了一种存储数据的角色,使得开发者可以很方便地将数据存储在本地文件中,不再需要依赖其他的数据库管理系统。 在C语言的开发中,SQLite减少了对复杂的数据库管理软件的依赖性,同时提升了对数据的处理速度和效率。它使用简单,易于维护,并且可以与其他编程语言相互通信,这意味着它在各种应用程序开发中都具有广泛的应用。使用SQLite,开发者可以轻松地创建、读取、更新和删除数据,使得我们可以更加专注于应用程序的逻辑开发。 因此,总的来说,SQLite在C语言中的作用主要是为了快速、简单地存储和管理数据,让开发者专注于业务逻辑的开发,提高软件开发的效率和质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值