嵌入式第四十一天(数据库)

发布于:2025-08-30 ⋅ 阅读:(18) ⋅ 点赞:(0)

一.

二.

三.

四.

五.

六.

1.

2.

七.

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

int main(int argc, const char *argv[])
{
	sqlite3 *pdb;
	int ret = sqlite3_open("./stu.db", &pdb);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_open error : %s\n", sqlite3_errmsg(pdb));
		return -1;
	}
	
	char *psql = "create table if not exists class3(id INTEGER, name TEXT, score REAL);";
	ret = sqlite3_exec(pdb, psql, NULL, NULL, NULL);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_exec error : %s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		return -1;
	}
	
	char sql[128] = {"insert into class3 values(1, \"zhangsan\", 99);"};
	ret = sqlite3_exec(pdb, sql, NULL, NULL, NULL);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_exec error : %s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		return -1;
	}


	sqlite3_close(pdb);

	return 0;
}
#include <stdio.h>
#include <sqlite3.h>

int flag = 0;

int callback(void *arg, int column, char **values, char **name)
{
//	printf("column = %d\n", column);
	if (0 == flag)
	{
		for (int i = 0; i < column; i++)
		{
			printf("%s ", name[i]);
		}
		printf("\n");
		flag = 1;
	}
	for (int i = 0; i < column; i++)
	{
		printf("%s ", values[i]);
	}
	printf("\n");

	return 0;
}

int main(int argc, const char *argv[])
{
	sqlite3 *pdb;
	int ret = sqlite3_open("./stu.db", &pdb);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_open error : %s\n", sqlite3_errmsg(pdb));
		return -1;
	}
	
	char *psql = "select * from data;";
	ret = sqlite3_exec(pdb, psql, callback, NULL, NULL);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "sqlite3_exec error : %s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		return -1;	
	}

	sqlite3_close(pdb);
	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到