65.9K
CodeProject 正在变化。 阅读更多。
Home

SQLDevKit:SQLite 的 C++ 面向对象包装器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.44/5 (6投票s)

2013年12月12日

LGPL3

2分钟阅读

viewsIcon

24721

downloadIcon

1961

SQL Developer Kit (SQD) 是 SQLite 3 数据库引擎的接口封装。

SQL Developer Kit (SQD) 用户指南

版本 0.5.0
日期:2013年12月11日
作者:Anthony Daniels
公司:Pyramid Solutions
http://www.pyramidsolutions.com

SQL Developer Kit (SQD) 是 SQLite 3 数据库引擎的接口封装。 SQD 提供了一系列方便的类,简化了对 SQLite 数据的访问。 库中包含四个类,SQLField, SQLRecord, SQLQuerySetSQLDatabase。 SQLite 是一个开源、零配置且可嵌入的结构化查询语言 (SQL) 数据库引擎。 存在许多 SQLite 封装器。 此封装器基于 Microsoft ADO 风格。 它主要作为 SQLite3 的学习练习而开发。 决定将其开源。 如果您发现错误,请告知作者。 以下段落概述了这些类的用法。

SQDDatabase 参考

可以通过设置文件路径,然后调用 OpenDB(); 来轻松打开数据库。

SQDDatabase m_objSqlDB;
this->m_objSqlDB.Set_strDBPathName(strFullPathName);
intRet = this->m_objSqlDB.OpenDB();

关闭数据库同样简单。

this->m_objSqlDB.CloseDB();

SQDDatabase 方法

public:
 //The following macro makes standard accessor functions for the protected member variables
 //The macro is from the HPC Template Library (HTL), and open source supplement the the STL.
 //For example:
 //std::string Get_strUserName(void)
 //void Set_strUserName(std::string varVal);
 
 //MEMBER VARIABLES///////////////////////////
 //!The string user name
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strUserName)
 //!The string password
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strPassword)
 //!The string database path name
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strDBPathName)
 //!The integer port number
 HTLMEMVAR_BYVAL_STYLE1(public,int,intPort)
        //!Boolean flage for is open  
 HTLMEMVAR_BYVAL_STYLE1(public,bool,blnIsOpen)
 //!The string last error
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strError)

         //MEMBER FUNCTIONS//////////////////////////
 public:
        //!Get the pointer to the RAW DATABASE
        sqlite3* Get_ptrDB(void);
                //!Open the database connection
        int OpenDB(void);
        //!Close the database Connection
        int CloseDB(void);
        //!Returns if the database is open
        bool IsOpen(void);
 //!Returns a list of table names for the database.
  std::vector<std::string> GetTableNames(void);

SQDQuerySet 参考

查询集是与 SQLite 数据库引擎的主要接口。 用户设置查询字符串,然后执行查询。 如果查询是 SELECT 语句,则检索到的记录将收集在查询的记录集中。 最佳实践中,用户使用 'rowid' 作为被检索的第一列执行查询非常重要。 除非特别要求,否则不返回 rowid 主键。

例如:SELECT rowid,* FROM strTableName
例如:SELECT rowid, col01, col02, col03 FROM strTableName

重要! SQLDevKit 假设记录集的第一列是主键。 否则,它没有将数据保存回数据库中正确行的手段。 以下是执行查询的示例。

SQDQuery m_objQuery;
std::string strTableName;
int intRes;
std::stringstream strCom;
strTableName; = "tblContactInfo";
strCom << "SELECT rowid,* FROM  " << strTableName.c_str();
m_objQuery.Set_ptrDatabase(&m_objSqlDB);
m_objQuery.Set_strQuery(strCom.str());
intRes = m_objQuery.Execute();

SQDQuerySet 方法

public:
 //!The string query to be executed
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strQuery)
 //!The integer code query result
 HTLMEMVAR_BYVAL_STYLE1(public,int,intResult)
 //!The string user name
 HTLMEMVAR_BYVAL_STYLE1(public, std::string, strTableName)
 //!The pointer to the native sqlite3 statement object
 HTLMEMVAR_BYVAL_STYLE1(public, sqlite3_stmt *, ptrStatement)
 //!Database Accessors
 void Set_ptrDatabase(SQDDatabase* ptrDB);
 SQDDatabase* Get_ptrDatabase(void);
 //!Promulgate table name to all records.  This is important to do before trying commits.
 //Execute does this automatically at the end of a query recordset build.
 void Set_AllTableNames(std::string strTableName);
 //!Promulgate database pointer to all records.  This is important to do before trying commits.
 //Execute does this automatically at the end of a query recordset build.
 void Set_AllDatabasePtrs(SQDDatabase* ptrDB);
 //!Executes the SQL Call to the database
 int Execute(void);
 //!Update All Records from the Database to the Recordset
 //This calls an sqlite3 reset function before executing the query again.
 int UpdateAll(void);
 //!Update Row from the Database to the Recordset
 int UpdateRow(size_t intRow);
 //!Commit All Changes to the database
 int CommitAll(void);
 //!Commit Row Changes to the database
 int CommitRow(size_t index);
 //!Get the recordset collection pointer
 SQDRecordCollection * GetRecordCollPtr(void);
 //!Returns the record at the desired index
 SQDRecord * AtRecord(size_t index);
 //!Returns the number of records in the recordset
 size_t CountRecords(void);
 size_t Size(void);
 //!Gets the number of columns in the recordset
 size_t CountColumns (void);
 //!Gets the specified column name
 std::string GetColumnName (int index);
 //!Get all column names 
 std::vector<std::string> GetColumnNames (void);
 //!Get the first record in the collection iterator
 SQDRecordIterator BeginRecord(void);
 //!Get the last record in the collection iterator
 SQDRecordIterator EndRecord(void);
 //!Get the next record in the list
 SQDRecordIterator NextRecord(void);
 //!Get the current cursor iterator
 SQDRecordIterator GetCursor(void);
 //!Create a Record with the supplied table definition
 SQDRecord * CreateRecord();
 //!Create a Record with the supplied record definition
 int InsertRecord(SQDRecord * ptrRec);
 //!Mark a Record for Deletion = TRUE
 int DeleteRecord(size_t index);
 //!Mark all Records for Deletion = TRUE
 int DeleteAllRecords(void);
 //!Mark a Record for Deletion = FALSE
 int UnDeleteRecord(size_t index);
 //!Mark all Records for Deletion = FALSE
 int UnDeleteAllRecords(void);
 //The Remove destroys the records in the LOCAL COPY ONLY, NOT THE DATABASE
 //!Delete the record with the provided index
 int RemoveRecord(size_t index);
 //!Delete all of the records
 int RemoveAllRecords(void);
 //The DeleteCommits destroy the records IN THE DATABASE
 //!Delete the record with the provided index
 int DeleteCommitRecord(size_t index);
 //!Delete all of the records
 int DeleteCommitAllRecords(void);

SQDRecord 参考

在像 SQLite 这样的表关系数据库的信息核心是记录,或行。 它是构成该集合中一行数据的一组字段。 SQLite 行具有一个唯一的 rowid,可以从中访问它们。 记录具有一组字段,用于集合中数据的每一列。 可以独立地更新各个行,并将其信息提交到数据库。 它们可以提交或更新一个字段或整个字段集合。 通过类似于 STL 的 AtField() 函数或通过迭代器访问字段。 该集合的管理方式类似于 STL 向量,具有类似的功能。

SQDRecord 方法

public:   
 //!Boolean Flag for New Record
 HTLMEMVAR_BYVAL_STYLE1(public,bool,blnNewRecord);
 //!Boolean Flag for Delete Record
 HTLMEMVAR_BYVAL_STYLE1(public,bool,blnDeleteRecord);
 //!Boolean Flag for whether the Record was Modified or Edited
 HTLMEMVAR_BYVAL_STYLE1(public,bool,blnModified);
 //!String Table Name this record belongs to
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strTableName)
 //!The pointer to the database object
 HTLMEMVAR_BYVAL_STYLE1(public,SQDDatabase*,ptrDatabase)
 public:
 //DB====>Active Memory
 //Updates all fields 
 int UpdateAll(void);
 //!Update the value for the selected field
 int UpdateField(size_t index);
 //Active Memory====>DB
 //!Commit all values for all fields
 int CommitAll(void);
 //!Commit the value of the selected field to the database
 int CommitField(size_t index);
 //!Get the field collection pointer
 SQDFieldCollection * GetFieldCollPtr(void);
 //CONTAINER ACCESSORS FOR TABLE COLLECTION
 //!Does the Table with strName Exist
 bool DoesFieldExist(std::string strName);
 //!Does the Table with strName Exist
 SQDField * GetField(std::string strName);
 //!Gets the field index number
 int GetFieldLoc(std::string strName);
 //!Returns the table at the desired index
 SQDField * AtField(size_t index);
 //!Returns the number of fields in the record
 int CountFields(void);
 //!Returns the 
 int Size(void);
 //!Get the FieldName at the index
 std::string GetFieldName(size_t index);
 //!Get the first table in the collection iterator
 SQDFieldIterator BeginField(void);
 //!Get the last table in the collection iterator
 SQDFieldIterator EndField(void);
 //!Get the next field iterator
 SQDFieldIterator NextField(void);
 //!Get the cursor directly
 SQDFieldIterator GetCursor(void);
 //!Create a Table with the supplied table definition
 SQDField * CreateField(std::string strName, SQDFieldType enmType);
 //!Delete the fields with the provided name
 int DeleteField(std::string strName);
 //!Delete all of the fields
 int DeleteAllFields(void);

SQDField 参考 

字段本质上是一个包含字段名称和字段值的名称值对容器。 SQLDevKit 处理所有数据类型,除了 BLOB。 此功能计划在稍后添加。 所有信息最初都以文本形式存储在字段中,并通过方便的 Get 和 Set 函数转换为值。

SQDField 方法

public:
 //!Field Name
 HTLMEMVAR_BYVAL_STYLE1(public,std::string,strName);
 //!Pointer to Parent Record
 HTLMEMVAR_BYVAL_STYLE1(public,SQDRecord*,ptrParent);
 //!Field Typebr /> HTLMEMVAR_BYVAL_STYLE1(public,SQDFieldType,objType);
 //!Set and Get for Node Value
 void Set_strValue(const std::string & strValue);
 void Set_strValue(const char * ptrCharString);
  void Set_strValue(bool blnVal);
 void Set_strValue(short shrtVal);
 void Set_strValue(unsigned short ushrtVal);
 void Set_strValue(int intVal);
 void Set_strValue(unsigned int uint);
 void Set_strValue(long lngVal);
 void Set_strValue(unsigned long ulngVal);
 void Set_strValue(float sngVal);
  void Set_strValue(double dblVal);
 void Set_strValue(char chrVal);
 void Set_strValue(unsigned char uchrVal);
 //!Set and Get for Node Value
 std::string Get_strValue(void);
 void Get_strValue(bool & blnValReturn);
 void Get_strValue(short & shrtValReturn);
 void Get_strValue(unsigned short & ushrtValReturn);
 void Get_strValue(int & intValReturn);
 void Get_strValue(unsigned int & uintValReturn);
 void Get_strValue(long & lngValReturn);
 void Get_strValue(unsigned long & ulngValReturn);
 void Get_strValue(float & sngValReturn);
 void Get_strValue(double & dblValReturn);
 void Get_strValue(char & chrValReturn);
 void Get_strValue(unsigned char & uchrValReturn);
 void Get_strValue(std::string & strValReturn);
© . All rights reserved.