C++Windows下SQLite简单封装

SQLiteCommand.h:

#pragma once
#include   
#include   
#include   
#include "sqlite3.h"

class SQLiteCommand
{
    public:

        SQLiteCommand();

        ~SQLiteCommand();

        bool Connect( LPCSTR pDataBaseName );

        bool Disconnect();

        bool IsExistsTable( LPCSTR pTableName );
        //for creating table.
        /*  eg.
            SQLiteCommand SQLite;
            ......
            if( false == SQLite.IsExistsTable( "player" ) )//if table player already exists CreateTable will be returned false.
            {
                SQLite.CreateTable( "player", "id int,name varchar( 16 )" );
            }
        */
        bool CreateTable( LPCSTR pTableName, LPCSTR pFieldScript );

        //eg. SelectTable( "player", "id,name", "id > 10", "id ASC" )
        bool SelectTable( LPCSTR pTableName, LPCSTR pSelectField = "*", LPCSTR pWhere = "", LPCSTR pOrder = "" );

        /* extends select had not been implemented yet.
        bool SelectTable();//pure script version.

        bool LeftJoinTable();

        bool RightJoinTable();

        bool InnerJoinTable();
        */

        bool InsertTable( LPCSTR pTableName, LPCSTR pInsertField, LPCSTR pInsertValue );

        bool UpdateTable( LPCSTR pTableName, LPCSTR pSetField, LPCSTR pWhere = "" );

        bool DeleteTable( LPCSTR pTableName, LPCSTR pWhere = "" );

        bool DropTable( LPCSTR pTableName );

        //////////////////////////////////////////////////////////////////////////
        //only to provide a detailed query for the method of the select table.
        /*  eg.
            SQLiteCommand SQLite;
            ......
            SQLite.SelectTable( "player", "id,name", "id > 10" ); //select id,name from player where id > 10
            //the cursor is at the top of the default.
            while( false == SQLite.IsEOF() )
            {
                INT id = atoi( GetField( "id" ) );
                LPCSTR name = GetField( "name" );
                SQLite.MoveNext();
            }
            //or cursor position in the end
            SQLite.MoveLast();
            while( false == SQLite.IsBOF() )
            {
                ......
                SQLite.MovePrevious();
            }
        */
        bool IsBOF();

        bool IsEOF();

        bool MovePrevious();

        bool MoveNext();

        bool MoveFirst();

        bool MoveLast();

        LPCSTR GetField( LPCSTR pFieldName );

    private:

        sqlite3 *mpCommand;

        std::vector< std::map< std::string, std::string > > mDataTable;

        size_t mIndex;
};
 SQLiteCommand.cpp: 
  

#include "SQLiteCommand.h"
#include 
#include 

#define SQLITE_SAFE_CLOSE( Ptr ){ if( nullptr != Ptr ){ sqlite3_close( Ptr ); Ptr = nullptr; } }    
#define SQLITE_SAFE_FREE( Ptr ){ if( nullptr != Ptr ){ sqlite3_free( Ptr ); Ptr = nullptr; } }    
#define SQLITE_SAFE_FREE_TABLE( Ptr ){ if( nullptr != Ptr ){ sqlite3_free_table( Ptr ); Ptr = nullptr; } }
#define SQL_SCRIPT_SIZE 1024

SQLiteCommand::SQLiteCommand()
    :mpCommand( nullptr )
    ,mIndex( -1 )
{}

SQLiteCommand::~SQLiteCommand()
{
    Disconnect();
}

bool SQLiteCommand::Connect( LPCSTR pDataBaseName )
{
    if( nullptr == pDataBaseName )
    {
        assert( false );
        return false;
    }

    if( SQLITE_OK != sqlite3_open( pDataBaseName, &mpCommand ) )
    {
        assert( false );
        return false;
    }

    return true;
}

bool SQLiteCommand::Disconnect()
{
    if( nullptr == mpCommand )
    {
        return true;
    }
    mIndex = -1;
    mDataTable.clear();
    SQLITE_SAFE_CLOSE( mpCommand );
    return true;
}

bool SQLiteCommand::IsExistsTable( LPCSTR pTableName )
{
    if( nullptr == pTableName )
    {
        assert( false );
        return false;
    }
    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    StringCbPrintfA( SQLScript, sizeof( SQLScript ), "select count(*) from sqlite_master where type='table' and name='%s'", pTableName );

    LPSTR pException = nullptr;
    LPCH *pData = nullptr;
    int RowCount = 0;  
    int ColumnCount = 0; 

    if( SQLITE_OK != sqlite3_get_table( mpCommand, SQLScript, &pData, &RowCount, &ColumnCount, &pException ) )
    {
        assert( !pException );
        return false;
    }

    const bool IsExists = atoi( pData[ ColumnCount ] ) > 0;
    SQLITE_SAFE_FREE_TABLE( pData );
    return IsExists;
}

bool SQLiteCommand::CreateTable( LPCSTR pTableName, LPCSTR pFieldScript )
{
    if( nullptr == pTableName || nullptr == pFieldScript )
    {
        assert( false );
        return false;
    }

    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    StringCbPrintfA( SQLScript, sizeof( SQLScript ), "create table %s(%s)", pTableName, pFieldScript );

    LPSTR pException = nullptr;
    if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
    {
        assert( !pException );
        return false;
    }

    return true;
}

bool SQLiteCommand::SelectTable( LPCSTR pTableName, LPCSTR pSelectField /*= "*"*/, LPCSTR pWhere /*= "" */, LPCSTR pOrder /*= "" */ )
{
    if( nullptr == pTableName || nullptr == pSelectField || nullptr == pWhere || nullptr == pOrder )
    {
        assert( false );
        return false;
    }

    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    if( 0 == *pWhere )
    {
        StringCbPrintfA( SQLScript, sizeof( SQLScript ), "select %s from %s", pSelectField, pTableName );
    }
    else
    {
        StringCbPrintfA( SQLScript, sizeof( SQLScript ), "select %s from %s where %s", pSelectField, pTableName, pWhere );
    }

    if( 0 != *pOrder )
    {
        StringCbCatA( SQLScript, sizeof( SQLScript ), " order by " );
        StringCbCatA( SQLScript, sizeof( SQLScript ), pOrder );
    }
         
    LPSTR pException = nullptr;
    LPCH *pData = nullptr;
    int RowCount = 0;  
    int ColumnCount = 0; 
        
    if( SQLITE_OK != sqlite3_get_table( mpCommand, SQLScript, &pData, &RowCount, &ColumnCount, &pException ) )
    {
        assert( !pException );
        return false;
    }

    mDataTable.clear();
    std::map< std::string, std::string > Row;
    for( int i = 0; i < RowCount; ++i )
    {
        for( int j = 0; j < ColumnCount; ++j )
        {
            const auto Insert = Row.insert( std::make_pair( pData[ j ], pData[ ColumnCount + i * RowCount + j ] ) );
            if( false == Insert.second )
            {
                assert( false );
                return false;
            }
        }
        mDataTable.push_back( Row );
        Row.clear();
    }

    mIndex = 0;

    SQLITE_SAFE_FREE_TABLE( pData );
    return true;
}

bool SQLiteCommand::InsertTable( LPCSTR pTableName, LPCSTR pInsertField, LPCSTR pInsertValue )
{
    if( nullptr == pTableName || nullptr == pInsertField || nullptr == pInsertValue )
    {
        assert( false );
        return false;
    }

    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    StringCbPrintfA( SQLScript, sizeof( SQLScript ), "insert into %s( %s )values( %s )", pTableName, pInsertField, pInsertValue );

    LPCH pException = nullptr;
    if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
    {
        assert( !pException );
        return false;
    }
    return true;
}

bool SQLiteCommand::UpdateTable(LPCSTR pTableName, LPCSTR pSetField, LPCSTR pWhere )
{
    if( nullptr == pTableName || nullptr == pSetField || nullptr == pWhere )
    {
        assert( false );
        return false;
    }

    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    if( 0 == *pWhere )
    {
        StringCbPrintfA( SQLScript, sizeof( SQLScript ), "update set %s from %s", pSetField, pTableName );
    }
    else
    {
        StringCbPrintfA( SQLScript, sizeof( SQLScript ), "update set %s from %s where %s", pSetField, pTableName, pWhere );
    }

    LPCH pException = nullptr;
    if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
    {
        assert( !pException );
        return false;
    }

    return true;
}

bool SQLiteCommand::DeleteTable( LPCSTR pTableName, LPCSTR pWhere )
{
    if( nullptr == pTableName || nullptr == pWhere )
    {
        assert( false );
        return false;
    }

    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    if( 0 == *pWhere )
    {
        StringCbPrintfA( SQLScript, sizeof( SQLScript ), "delete from %s", pTableName );
    }
    else
    {
        StringCbPrintfA( SQLScript, sizeof( SQLScript ), "delete from %s where %s", pTableName, pWhere );
    }

    LPCH pException = nullptr;
    if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
    {
        assert( !pException );
        return false;
    }

    return true;
}

bool SQLiteCommand::DropTable( LPCSTR pTableName )
{
    if( nullptr == pTableName )
    {
        assert( false );
        return false;
    }
    CHAR SQLScript[ SQL_SCRIPT_SIZE ];
    StringCbPrintfA( SQLScript, sizeof( SQLScript ), "drop table %s", pTableName );
        
    LPCH pException = nullptr;
    if( SQLITE_OK != sqlite3_exec( mpCommand, SQLScript, nullptr, nullptr, &pException ) )
    {
        assert( !pException );
        return false;
    }
    return true;
}

bool SQLiteCommand::IsBOF()
{
    if( mDataTable.empty() )
    {
        return true;
    }
    return -1 == mIndex;
}

bool SQLiteCommand::IsEOF()
{
    if( mDataTable.empty() )
    {
        return true;
    }
    return mDataTable.size() == mIndex;
}

bool SQLiteCommand::MovePrevious()
{
    if( IsBOF() )
    {
        assert( false );
        return false;
    }
    --mIndex;
    return true;
}

bool SQLiteCommand::MoveNext()
{
    if( IsEOF() )
    {
        assert( false );
        return false;
    }
    ++mIndex;
    return true;
}

bool SQLiteCommand::MoveFirst()
{
    if( mDataTable.empty() )
    {
        return true;
    }
    mIndex = 0;
    return true;
}

bool SQLiteCommand::MoveLast()
{
    if( mDataTable.empty() )
    {
        return true;
    }
    mIndex = mDataTable.size() - 1;
    return true;
}

LPCSTR SQLiteCommand::GetField( LPCSTR pFieldName )
{
    if( nullptr == pFieldName )
    {
        assert( false );
        return nullptr;
    }
    const auto &Row = mDataTable[ mIndex ];
    const auto &Cell = Row.find( pFieldName );
    return Row.end() == Cell ? nullptr : Cell->second.c_str();
}

后续我会不断的完善这个类,包括数据库密码 数据的加密和解密 内部扩展GetField 支持大部分基本数据类型,当然自己也可以去扩展

你可能感兴趣的:(SQLite,Windows,Windows,SQLite,封装)