持久化存储SQLite的两种C++封装(easySQLite 和 cppSQLite)

本文介绍了SQLite,一个轻量级、无服务器、零配置的SQL数据库引擎,并探讨了两种C++封装库——easySQLite和cppSQLite。easySQLite以其面向对象的特性、异常处理和灵活性受到青睐,而cppSQLite则提供了C++的包装器。文章通过简单的类介绍展示了如何使用这两个库进行数据库操作。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

                       

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is the most widely deployed database in the world with more applications than we can count, including several high-profile projects.

http://www.sqlite.org/

SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。

由于使C API, 有很多优秀的对SQLITE进行的封装,今天就介绍两种 easySQLitecppSQLite

easySQLite

为什么easySQLite优于其他封装?
https://code.google.com/archive/p/easysqlite/ 哈哈,不敢苟同,反正挺好用的)

elegant, objective solution (优雅的面向对象解决方案)
explicit naming and calling  (显式命名和调用)
uses exceptions or methods return values (使用异常以及方法返回值)
clear, understandable usage (容易理解)
flexible and expandable (灵活而且可扩展)
strongly tested (tests included) (经过强测试)

easySQLite里面有一些类:
这里写图片描述

这里就简单展示几个而已:

SqlCommon.h

//// Copyright (C) 2010 Piotr Zagawa//// Released under BSD License//#pragma once#include <string>#include <vector>#include <stdio.h>#include <time.h>namespace sql{
   //field state enums//--------------------------enum field_use{    FIELD_DEFAULT,    FIELD_KEY,    DEFINITION_END,};enum field_type{    type_undefined,    type_int,    type_text,    type_float,    type_bool,    type_time,};enum field_flags{    flag_none = 0,    flag_not_null = 1,    flag_primary_key = 2,};//common data types//--------------------------//stringtypedef std::string string;//integer#if defined(_MSC_VER) || defined(__BORLANDC__)typedef __int64 integer;#elsetypedef long long int integer;#endif//timeclass time{
   private:    time_t _value;private:    string format(const char* format);public:    time();    time(const time& value);    time(integer value);    time& operator=(const time& value);    bool operator==(const time& value);    static time now();public:    double diff(time& value);    time_t get();    void addValue(integer value);    void addMinutes(integer count);    void addHours(integer count);    void addDays(integer count);public:    integer asInteger();    string asString();    string asTimeString();    string asDateString();};//common exception classclass Exception{
   private:    string _msg;public:    Exception(string msg)        : _msg(msg)    {    };    string msg()    {        return _msg;    }};//comment this directive to disable exceptions//#define USE_EXCEPTIONS#ifndef THROW_EXCEPTION    #ifdef USE_EXCEPTIONS        #define THROW_EXCEPTION(msg) throw Exception(msg);    #else        #define THROW_EXCEPTION(msg)    #endif#endif//utils//--------------------------class log{
   public:    log(std::string s)    {        std::string text = s;        text += "\r\n";        printf("%s",text.c_str());    }};string intToStr(int value);string intToStr(integer value);string quoteStr(string value);string binToHex(const char* buffer, int size);string generateSHA(string& value);string& trimleft(string& s);string& trimright(string& s);string& trim(string& s);string trim(const string& s);void listToVector(string s, std::vector<string>& vector, const char* sep = ",");//sql eof};
  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159

用Field 对象定义一个表结构
SqlField.h

#pragma once#include "SqlCommon.h"namespace sql{class Field{
   public:    friend class FieldSet;private:    string _name;    field_use _use;    field_type _type;    int _index;    int _flags;public:    Field(field_use use);    Field(string name, field_type type, int flags = flag_none);    Field(const Field& value);public:    bool isKeyIdField();    bool isEndingField();public:    int getIndex();    string getName() const;    string getTypeStr();    field_type getType();    bool isPrimaryKey();    bool isNotNull();public:    string getDefinition();    static Field* createFromDefinition(string value);};//sql eof};
  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

先定义一个Database对象,然后打开进行操作
SqlDatabase.h

//// Copyright (C) 2010 Piotr Zagawa//// Released under BSD License//#pragma once#include "sqlite3.h"#include "SqlCommon.h"namespace sql{class Database{
   private:    sqlite3* _db;    string _err_msg;    int _result_open;public:    Database(void);    ~Database(void);public:    sqlite3* getHandle();    string errMsg();    bool open(string filename);    void close();    bool isOpen();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值