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

本文介绍了SQLite,一个广泛部署的、轻量级的、支持ACID的数据库引擎,并探讨了两种C++封装库——easySQLite和cppSQLite。easySQLite以面向对象的方式提供清晰、易用的API,而cppSQLite则是来自CodeProject的SQLite3库的C++包装器。
摘要由CSDN通过智能技术生成

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
//--------------------------

//string
typedef std::string string;


//integer
#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 integer;
#else
typedef long long int integer;
#endif


//time
class 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 class
class 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
};

用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
};

先定义一个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();

public:
    bool transactionBegin();
    bool transactionCommit();
    bool transactionRollback();

};


//sql eof
};

通过Table对象来操作数据库了
SqlTable.h

//
// Copyright (C) 2010 Piotr Zagawa
//
// Released under BSD License
//

#pragma once

#include "sqlite3.h"
#include "SqlCommon.h"
#include "SqlFieldSet.h"
#include "SqlRecordSet.h"


namespace sql
{

class Table
{
private:
    sqlite3* _db;
    string _tableName;
    RecordSet _recordset;

public:
    Table(sqlite3* db, string tableName, Field* definition);
    Table(sqlite3* db, string tableName, FieldSet* fields);

public:
    string name();
    string getDefinition();
    string toString();
    string errMsg();
    FieldSet* fields();
    sqlite3* getHandle();

public:
    bool create();
    bool exists();
    bool remove();
    bool truncate();

public:
    bool open();
    bool open(string whereCondition);
    bool open(string whereCondition, string sortBy);
    bool query(string queryStr);
    int totalRecordCount();

public:
    int recordCount();
    Record* getRecord(int record_index);
    Recor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一苇渡江694

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

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

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

打赏作者

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

抵扣说明:

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

余额充值