How to create a database connection via C ++

In C ++, how can I establish a SQL connection to store data in an SQL database?

+1


source to share


5 answers


You should take a look at the C preprocessors that traditionally exist with databases ( ecpg for postgres, Pro * C for oracle ... which allows you to insert straight SQL directly into your source files) or the original system for mysql. ECPG will work from C ++, i.e. / not suitable for some other preprocessors ...



+3


source


IF you target Windows then you can use ODBC.



+3


source


you can try wxSqlite with SQLite as your database. This gives you an open source / C ++ file to get started with.

In general - you should get some kind of library that will offer you the functionality you need. All major database vendors should offer at least a C library. Most of the time, you end up with a C ++ library or wrapper for C.

+1


source


Use SQLAPI ++ is a cross platform platform and supports MS SQL Server, Oracle, Postgres and others. Very easy to use.

http://www.sqlapi.com/

0


source


If you are using window targeting, you can always use the import option.

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename( "EOF", "EndOfFile" )

      

Then you can make a SQL wrapper.

Small example, just to close the connection

// H
class CExtAdoDatabase
{

public:
    CExtAdoDatabase( const char* p_cConnectString="", const char* p_cUsername="", const char* p_cPwd="" );
    virtual ~CExtAdoDatabase();

    bool Open( const char* p_cConnectString="", const char* p_cUsername="", const char* p_cPwd="" );
    bool Close();



private:
    HRESULT _hInitRes;
    bool _bIsValid;

    _ConnectionPtr *_p_pConnection;
};



// CPP
CExtAdoDatabase::CExtAdoDatabase( const char* p_cConnectString, const char* p_cUsername, const char* p_cPwd ) : _hInitRes( CoInitialize( NULL ))
{
    _p_pConnection = new _ConnectionPtr( "ADODB.Connection" );

    if( FAILED( _hInitRes ))
            _bIsValid = false;
    else
    {
        _bIsValid = true;
        (*_p_pConnection)->ConnectionTimeout=0;
        (*_p_pConnection)->CommandTimeout=0;

        if( p_cConnectString != NULL && strlen(p_cConnectString) )
        {
            _bstr_t scs( p_cConnectString );
            _bstr_t susr( p_cUsername );
            _bstr_t spwd( p_cPwd );
            (*_p_pConnection)->Open( scs, susr, spwd, NULL );
        }
    }
}
CExtAdoDatabase::~CExtAdoDatabase()
{
    Close();
    delete _p_pConnection;
    CoUninitialize();
}

bool CExtAdoDatabase::Open( const char* p_cConnectString, const char* p_cUsername, const char* p_cPwd )
{
    if(_bIsValid)
    {
        _bstr_t scs( p_cConnectString );
        _bstr_t susr( p_cUsername );
        _bstr_t spwd( p_cPwd );
        return ((*_p_pConnection)->Open( scs, susr, spwd, NULL ) == S_OK);
    }
    else
        return false;
}

bool CExtAdoDatabase::Close()
{
    if( _bIsValid )
    {
        if( (*_p_pConnection)->GetState() == adStateOpen )
            return !!(*_p_pConnection)->Close();
        else
            return true;
    }
    else
        return false;
}

      

0


source







All Articles