【C语言+sqlite3 API接口】实现水果超市

这篇具有很好参考价值的文章主要介绍了【C语言+sqlite3 API接口】实现水果超市。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

实验内容:

假如我家开了个水果超市,有以下水果,想实现自动化管理,扫描二维码就能知道当前的水果状态,进货几天了,
好久需要再次进货,那些水果畅销,那些水果不畅销,那些水果春夏秋冬的价格波动,好,那么现在我想将这些信息保存在数据库中,那么我应该怎么做?

超市每天水果都有进货和卖出嘛,水果的价格随着季节和天气也会有波动,顾客也会看一下每天水果的价格的嘛, 所以要求,利用数据库完成水果店各种水果的增(进货)删(卖出)改(波动)查(看价格)功能。
并将进出货的时间和顾客光顾的时间记录到数据库中保存。

相关API

可以看看3)sqlite3 数据库 C语言
API

实现流程:

首先创建一张数据表fruit,除了需要指定每条记录的唯一标识id外,还要有水果品类(name)、存量(weight)、价格(price)、最近交易时间(time)等字段。
每一次操作,都会改变表中的数据内容,这些我们通过API来实现:各种水果的增(进货)删(卖出)改(波动)查(看价格)功能。并将进出货的时间和顾客光顾的时间记录到数据库中保存。

实现代码:

fruit.h

#ifndef _FRUIT_H_
#define _FRUIT_H_

#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define N 128
#define DATABASE "fruitery.db"
#define TABLE "fruit"

int do_insert(sqlite3 *db, char *buf);
int do_query(sqlite3 *db);
int do_update_weight(sqlite3 *db, char * buf);
int do_update_price(sqlite3 *db, char * buf);
int do_delete(sqlite3 *db, char *buf);
int do_delete_sort(sqlite3 *db, char * buf, int id);

#endif

fruit.c

#include "fruit.h"

int do_insert(sqlite3 *db, char *buf) {
	char *errmsg;
	char sql[N] = {};
	char name[N] = {};
	float weight;
	float price;

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

	printf("Input weight:");
	scanf("%f", &weight);
		
	printf("Input price:");
	scanf("%f", &price);

	sprintf(sql, "insert into '%s' (name, weight, price, time) values('%s', '%.3f', '%.3f', '%s')", TABLE, name, weight, price, buf);
	if ( sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
		printf("Error: %s\n", errmsg);
	} else {
		printf("insert done.\n");
	}

	return 0;
}

int callback (void* arg,int f_num ,char** f_value,char** f_name) {
	int i;

	for(i = 0; i < f_num; i++) {
		printf("%-8s", f_value[i]);
	}
	return 0;
}

int do_query(sqlite3 *db) {
	char *errmsg;
	char sql[N] = {};

	sprintf(sql, "select * from '%s'", TABLE);
	if ( sqlite3_exec(db, sql, callback, NULL, &errmsg) != SQLITE_OK) {
		printf("Error: %s\n", errmsg);
	} else {
		printf("query done.\n");
	}
	return 0;
}

int do_update_weight(sqlite3 *db, char * buf)
{
	char *errmsg;
	char sql[N] = {};
	float weight;
	int id;

	printf("Input id:");
	scanf("%d", &id);

	printf("Input weight:");
	scanf("%f", &weight);

	sprintf(sql, "update '%s' set weight = '%.3f', time = '%s' where id = %d", TABLE, weight, buf, id);
	if ( sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
		printf("Error: %s\n", errmsg);
	} else {
		printf("update weight done.\n");
	}
	return 0;
}

int do_update_price(sqlite3 *db, char * buf)
{
	char *errmsg;
	char sql[N] = {};
	float price;
	int id;

	printf("Input id:");
	scanf("%d", &id);

	printf("Input price:");
	scanf("%f", &price);

	sprintf(sql, "update '%s' set price = '%.3f', time = '%s' where id = %d", TABLE, price, buf, id);
	if ( sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
		printf("Error: %s\n", errmsg);
	} else {
		printf("update price done.\n");
	}
	return 0;
}

int do_delete(sqlite3 *db, char * buf) 
{
	char *errmsg;
	char sql[N] = {};
	int id;

	printf("Input id:");
	scanf("%d", &id);

	do_delete_sort(db, buf, id );
	sprintf(sql, "delete from '%s' where id = %d", TABLE, id);
	if ( sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
		printf("Error: %s\n", errmsg);
	} else {
		printf("delete done.\n");
	}
	return 0;
	
}

#define NEW_TABLE "new_table"
int do_delete_sort(sqlite3 *db, char *buf, int id) 
{
	char *errmsg;
	char sql[N] = {};

	//创建一张临时的新表格
	sprintf(sql, "create table '%s'(id integer  primary key autoincrement, name char, weight float, price float, time char);", NEW_TABLE);
	sqlite3_exec(db, sql, NULL, NULL, &errmsg);
	memset(sql, 0, sizeof(sql));

	//
	sprintf(sql, "insert into '%s' select * from '%s' where id = %d;",NEW_TABLE, TABLE, id);
	sqlite3_exec(db, sql, NULL, NULL, &errmsg);
	memset(sql, 0, sizeof(sql));

	//删除原有的表
	sprintf(sql, "drop table '%s'", TABLE);
	sqlite3_exec(db, sql, NULL, NULL, &errmsg);
	memset(sql, 0, sizeof(sql));

	//将新表的名字改成原有的旧表的名字
	sprintf(sql, "alter table '%s' rename to '%s'", NEW_TABLE, TABLE);
	sqlite3_exec(db, sql, NULL, NULL, &errmsg);
	memset(sql, 0, sizeof(sql));
	return 0;
}

test.c

#include "fruit.h"

int main (int argc, char *argv[]) {
	sqlite3 *db;
	char *errmsg;
	char buf[N] = {};
	char sql[N] = {};
	int n;

	time_t t;
	struct tm *tp;

//打开数据库文件 
	if ( sqlite3_open(DATABASE, &db) != SQLITE_OK ) {
		printf("%s\n", errmsg);
		return -1;
	} else {
		printf("open DATABASE success.\n");
	}
//创建一张数据库表格
	sprintf(sql, "create table '%s'(id integer  primary key autoincrement, name char, weight float, price float, time char);", TABLE);
	if ( sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
		printf("exec :%s\n", errmsg);
	} else {
		printf("create table success.\n");
	}

	//时间
	time(&t);
	tp = localtime(&t);
	sprintf(buf,"%d-%02d-%02d %02d:%02d:%02d\n",
			tp->tm_year+1900, tp->tm_mon+1,tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
	printf("%s\n", buf);

	while (1) {
		printf("-------------------------------------------\n");
		printf("1: insert 2:query 3:trade 4:update 5:delete 6:quit\n");
		printf("-------------------------------------------\n");
		printf("Please select:");
		scanf("%d", &n);

		switch(n) 
		{
		case 1: 
			do_insert(db, buf);
			break;
		case 2:
			do_query(db);
			break;
		case 3:
			do_update_weight(db, buf);
			break;
		case 4:
			do_update_price(db, buf);
			break;
		case 5:
			do_delete(db, buf);
			break;
		case 6:
			printf("main exit.\n");
			sqlite3_close(db);
			exit(0);
			break;
		default :
			printf("Invalid data,\n");
		}
	}
	return 0;
}

实现结果:

【C语言+sqlite3 API接口】实现水果超市,c语言,sqlite,oracle文章来源地址https://www.toymoban.com/news/detail-584396.html

到了这里,关于【C语言+sqlite3 API接口】实现水果超市的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • sqlite3数据库的实现

    sqlite3代码实现数据库的插入、删除、修改、退出功能

    2024年02月12日
    浏览(44)
  • 【Linux c】 Sqlite3 操作与功能实现

    一、Sqlite3 数据类型 二、Sqlite3 常用指令操作 三、Sqlite3 API 1. sqlite3_open ( ) 2. sqlite3_errmsg ( ) 3. sqlite3_close ( ) 4. sqlite3_exec ( ) 5. sqlite3_get_table ( ) 6. sqlite3_prepare_v2 ( ) 7. sqlite3_bind ( ) 8. sqlite3_step ( ) 9. sqlite3_finalize ( ) 四、Sqlite3 事务机制 (性能优化) 1. 事务 2. 锁状态 3. SQLITE_BUSY 五、

    2023年04月17日
    浏览(35)
  • 通过C实现sqlite3操作,(增删改查),导入电子词典

    一、插入 二、导入电子词典

    2024年02月12日
    浏览(31)
  • 基于Qt数据库项目实现(Sqlite3为例)|考查数据库、表格(QTableView 显示)(进阶)

    01 数据库表格(QTableView 显示) 本小节设计一个生活中的例子,使用数据库修改/查询员工的编号、姓名、年龄、性别与照片信息。 本例将数据库的内容显示到 QTableView 上。如果只是简单的显示数据库的内容到QTableView 上,可以使用下面的方法,此方法 QTableView 上可以看到

    2024年02月20日
    浏览(34)
  • Java学习——水果超市商品管理系统

    案例介绍: 水果超市管理系统包括两个界面:系统欢迎界面和超市货物管理界面,在系统欢迎界面中,通过单击“进入系统”按钮,进入超市货物管理界面。在超市货物管理界面中,可以对水果信息实现具体的操作。例如,每当有新水果运送到超市时,就需要系统管理人员在

    2024年02月11日
    浏览(29)
  • Python数据库模块(sqlite3,SQLite3)

    创建数据库:在控制台sqlite3 name sqlite3.connect(database [,timeout ,other optional arguments]) 打开数据库;如果指数据库存在则返回一个连接对象,如果不存在则会创建一个数据库; connection.cursor() 创建一个cursor; cursor.execute(sql) 执行一个sql语句,该语句可以被参数化; connection.execut

    2024年03月19日
    浏览(43)
  • 基于Qt数据库项目实现(Sqlite3为例)|考查数据库、绘制(画家)、事件等知识点(进阶)

    坚持最初的梦想,扬帆起航,乘风破浪,永不言败。 01 数据库 数据库是什么?简易言之,就是保存数据的文件。可以存储大量数据,包括插入数据、更新数据、截取数据等。用专业术语来说,数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计

    2024年02月19日
    浏览(32)
  • 【Sqlite3】maraidb和sqlite3部分命令操作区别

    maraidb和sqlite3部分命令操作区别记录 在实现我的视频点播系统项目时,我尝试封装了两种数据库的调用逻辑 mysql(maraidb) sqlite3 这里封装sqlite3的原因是,sqlite3主要针对的就是 嵌入式 数据库,其性能可能不如mysql,但是就好在可以 带着走 。安装也很方便,内存占用相对于

    2024年02月09日
    浏览(43)
  • 02.sqlite3学习——嵌入式数据库的基本要求和SQLite3的安装

    目录 嵌入式数据库的基本要求和SQLite3的安装 嵌入式数据库的基本要求 常见嵌入式数据库 sqlite3简介 SQLite3编程接口模型 ubuntu 22.04下的SQLite安装 (1)安装SQLite3软件 sudo apt-get install sqlite3 (2)安装库文件 sudo apt-get install libsqlite3-dev 安装成功后输入sqlite3查看 (3)安装sqlite3可

    2024年02月11日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包