C++ SQlite3系列操作

Sqlte3,很好的一个轻量级,微型数据库,也是一个文件型关系数据库,关于它的更多定义和解释,网上一搜就是一大把,这里就不赘述了。

 SQlite3 ,项目中常用于带数据库用户登陆权限,又需求有私密性保护,或者存储一些一定日期内的生产数据,后期需要做图表,实现UPH等各种数据分析,客户又不想弄台专门的SQLServer,只想简单一些,sqlte3则是上选,【Redis似乎也可以,它更倾向于实时交互性要求高的场合,比如实时采集设备数据,IOT物联存储显示的那种比较合适】

   sqlte3的使用较多的是就是增、删、改、查等基础功能,再复杂一点就是去重复,左联、右联等,这些sql语法,学起来很容易,上手也快,百度上一搜也是一大把,这里我的重点是把C_++ 的代码总结一下,以备忘:

  

#pragma once
#include <string.h>
#include <string>
#include "ExcelTable.h"
#include <vector>
#include <set>
#include <map>
//#include <atlstr.h>
#ifdef LIBSQLITE3OPERATOR_EXPORTS
#define LIB_SQLHELPER_API __declspec(dllexport)
#else
#define LIB_SQLHELPER_API __declspec(dllimport)
#endif

typedef struct Login
{
    Login() { memset(this, 0, sizeof(Login)); };
    std::wstring account;      // 登陆用户名
    std::wstring password;     // 密码
    int   pwd_state;    // 是否记住密码,1记住,0不记住
} login_t;

class LIB_SQLHELPER_API TSQLHelper
{
public:
    TSQLHelper();
    ~TSQLHelper();
public:        
    void InitializeDB();
    bool addItemtoCounterTable(CounterRecord reoardTable);
    //mindex:点料位置记录
    bool addItemtoCounterTable(std::string strUPSN, std::string strMaterID,
        std::wstring matericSpec, const std::string counterNO, const std::string mindex);
    
    //获取所有数据
    void getAllCounterTbDataList(std::vector<CounterRecord>& recordList);
    void getAllMaterialTypesList(std::set<std::wstring>& materTypeList, std::string startDatTime, std::string endDatTime);
    
    void getConditionCounterTbList(std::vector<CounterRecord>& recordList,
        std::string startDatTime, std::string endDatTime, std::string materSpec,
        std::string materID);

    //物料统计数据表:每种物料各点检了多次少,一种有多少盘物料
    void getAllMaterialStaticList(std::vector<MaterialStaticRecord>& materStaticList,
        std::string startDatTime, std::string endDatTime);
    //为饼图数据查询结果值,进行分析,upsn号才是唯一的
    void getPieChartData(std::string upsn, std::string strDTime,
        std::string endDTime, std::multimap<std::string, int>& dstPieMap);
    //线型数据,过滤重复数据
    void getLineChatDataList(std::string materID, std::string startDatTime, std::string endDatTime,
        std::map<std::string, int>& dstLineMapList);

    bool deleteCounterRecord(std::string strDateTime);

    /*
     login登陆 专用API:
    */
    int  addItemToLoginTable(login_t login);
    //支持中文简繁体 0:添加成功,-1 失败,1:用户表不存在  2: 重复添加
    int  addItemToLoginTable(std::wstring muser, std::wstring mPwd);
    bool isLoginValided(std::wstring user, std::wstring pwd);
    //查询user帐户是否存在
    bool isUserExist(std::wstring user);
    //用于修改密码:-1: user不存在于表中,0 执行成功,1:执行update失败
    int updateUserWithPwd(std::wstring user, std::wstring pwd);
    void getLoginUserList(std::vector<std::wstring>& userList);
    
    // 操作:释放已分配的内存(login_t*)
    //      获得一条登陆表信息
    // 前置条件:login_t指向一个初始化的结构体
    void getItemFromLoginTable(login_t *login);            
    int  deleteLoginTable(int id);
    void sortByYear(std::string startDatTime, std::string endDatTime, std::map<std::wstring, int>& dstResultMap);
    void sortByMonth(std::string startDatTime, std::string endDatTime, std::map<std::wstring, int>& dstResultMap);
    void sortByWeek(std::string startDatTime, std::string endDatTime, std::map<std::wstring, int>& dstResultMap);
public:
    static TSQLHelper* ginstance();
private:
    bool createRecordTable();
    bool createLoginTable();
    // 操作:初始化数据库和表    
    // 操作:释放数据库
    void DestructDB();
public:
    std::string wstringtoUTF8char(const std::wstring wstr);
    std::string wstring2UTF8String(std::wstring strc);
    //std::string cstring2UTF8String(CString strc);
    std::string wsto_string(const std::wstring& input);
    std::string GetApplicationStringPath();
};
#define  exeDB TSQLHelper::ginstance()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海木漄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值