【C语言+sqlite3 API接口】实现水果店管理

2024-02-02 04:59

本文主要是介绍【C语言+sqlite3 API接口】实现水果店管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 实验内容:

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


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

 

相关API:

我们使用sqlite3管理数据,使用到的C语言编程接口(API)有

int sqlite3_open(const char *filename,  sqlite3 **ppDb);
    功能:打开数据库
    参数:filename  数据库名称
               ppdb      数据库句柄
    返回值:成功为0 SQLITE_OK ,出错 错误码

    int sqlite3_close(sqlite3* db);
    功能:关闭数据库
    参数:
    返回值:成功为0 SQLITE_OK ,出错 错误码

   const char *sqlite3_errmsg(sqlite3*db);
    功能:得到错误信息的描述
 
   int sqlite3_exec(sqlite3* db, const char *sql,int (*callback)(void* arg,int,char**,char**),  void * arg,  char **errmsg );
  功能:执行一条sql语句
  参数:db  数据库句柄
            sql sql语句
            callback  回调函数,只有在查询时,才传参
            arg      为回调函数传递参数
            errmsg  错误消息
  返回值:成功 SQLITE_OK

查询回调函数:
int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name),  /* Callback function */
功能:查询语句执行之后,会回调此函数
参数:arg   接收sqlite3_exec 传递来的参数
      ncolumns 列数
      f_value 列的值得地址
      f_name   列的名称
返回值:0,

 

实现流程:

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

 

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <time.h>#define  DATABASE  "mysql.db"
#define TABLES "fruit"
#define  N  128int do_insert(sqlite3 *db,char *buf);
int do_update_weight(sqlite3 *db,char *buf);
int do_update_price(sqlite3 *db);
int do_query(sqlite3 *db);
int callback(void *arg, int f_num, char ** f_value, char ** f_name);int main(int argc, const char *argv[])
{sqlite3 *db;char *errmsg;int n;char sql[128] = {};char buf[128] = {};time_t t;struct tm *tp;if(sqlite3_open(DATABASE, &db) != SQLITE_OK){printf("%s\n", sqlite3_errmsg(db));return -1;}else{printf("open DATABASE success.\n");}sprintf(sql,"create table if not exists '%s' (id integer primary key autoincrement,name char,weight float,price float,time char);",TABLES);if(sqlite3_exec(db, sql,NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("Create or open 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",buf);while(1){puts("-----------------------------------------");do_query(db);puts("-----------------------------------------");puts("");printf("********************************************\n");printf("1: insert  2:query  3:trade 4:update 5: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);break;case 5:printf("main exit.\n");sqlite3_close(db);exit(0);break;default :printf("Invalid data n.\n");}}return 0;
}int do_insert(sqlite3 *db,char *buf)
{char name[32] = {};float weight;float price;char sql[N] = {};char *errmsg;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')",TABLES, name, weight, price,buf);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("Insert done.\n");}return 0;
}int do_update_weight(sqlite3 *db,char *buf)
{int id;char sql[N] = {};char *errmsg;float weight;printf("Input id:");scanf("%d", &id);printf("Input weight:");scanf("%f", &weight);sprintf(sql, "update '%s' set weight='%f',time='%s' where id=%d",TABLES, weight,buf,id);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("Delete done.\n");}return 0;
}int do_update_price(sqlite3 *db)
{float price;int id;char sql[N] = {};char *errmsg;printf("Input id:");scanf("%d", &id);printf("Input alter price:");scanf("%f", &price);sprintf(sql, "update '%s' set price='%f' where id=%d",TABLES , price,id);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("%s\n", errmsg);}else{printf("update done.\n");}return 0;
}int callback(void *arg, int f_num, char ** f_value, char ** f_name)
{int i = 0;for(i = 0; i < f_num; i++){//	printf("%-8s %s", f_value[i], f_name[i]);printf("%-8s", f_value[i]);}putchar(10);puts("-----------------------------------------");return 0;
}int do_query(sqlite3 *db)
{char *errmsg;char sql[N] = {};sprintf(sql,"select * from '%s';",TABLES);if(sqlite3_exec(db, sql, callback,NULL , &errmsg) != SQLITE_OK){printf("%s", errmsg);}else{printf("select done.\n");}return 0;
}

 

实验结果:

这篇关于【C语言+sqlite3 API接口】实现水果店管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/669547

相关文章

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat