CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Location
    Mysore,Karnataka
    Posts
    3

    MySQL Connectivity

    Hai friends,
    I have installed MySQL server 4.0.12 to my system on linux platform. We are using C program to connect to the MySQL database. Can the C code be implemented from C++ without and problems ?
    If yes do we have to make some changes ?
    Please reply me.

  2. #2
    Join Date
    Mar 2003
    Location
    mysore, india
    Posts
    21
    goto www.mysql.com site.

    hope u understand
    j.sakthivel

  3. #3
    Join Date
    Dec 2004
    Posts
    6

    Post Re: MySQL Connectivity

    Quote Originally Posted by sharanu
    Hai friends,
    I have installed MySQL server 4.0.12 to my system on linux platform. We are using C program to connect to the MySQL database. Can the C code be implemented from C++ without and problems ?
    If yes do we have to make some changes ?
    Please reply me.
    OleInitialize( NULL );

    OleUninitialize();

    IDBInitialize* pIDBInitialize = NULL;
    IDBCreateCommand* pIDBCreateCommand = NULL;
    ICommand* pICommand = NULL;
    ICommandText* pICommandText = NULL;


    IDBInitialize* pIDBInit = NULL;
    IDBProperties* pIDBProperties = NULL;
    DBPROPSET dbPropSet[1];
    DBPROP dbProp[1];


    hr = CoCreateInstance( CLSID_MySqlProv,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IDBInitialize,
    (void **)&pIDBInit );


    VariantInit(&(dbProp[0].vValue));


    dbPropSet[0].rgProperties = &dbProp[0];
    dbPropSet[0].cProperties = 1;
    dbPropSet[0].guidPropertySet = DBPROPSET_DBINIT;
    dbProp[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
    dbProp[0].dwOptions = DBPROPOPTIONS_REQUIRED;
    dbProp[0].colid = DB_NULLID;
    V_VT(&(dbProp[0].vValue)) = VT_BSTR;
    V_BSTR(&(dbProp[0].vValue)) = SysAllocString( DEF_MYSQL_CONN );

    hr = pIDBInit->QueryInterface( IID_IDBProperties, (void**)&pIDBProperties);
    hr = pIDBInit->Initialize();

    *ppIDBInitialize_out = pIDBInit;

    IDBCreateSession* pIDBCreateSession;
    IDBCreateCommand* pIDBCreateCommand;


    hr = pIDBInitialize->QueryInterface( IID_IDBCreateSession, (void**)&pIDBCreateSession);
    hr = pIDBCreateSession->CreateSession( NULL, IID_IDBCreateCommand, (IUnknown**)&pIDBCreateCommand );
    *ppIDBCreateCommand_out = pIDBCreateCommand;

    ICommand* pICommand = NULL;
    hr = pIDBCreateCommand->CreateCommand (NULL, // pUnkOuter - we are not aggregating
    IID_ICommand, // riid - interface we want on the command object
    (IUnknown**)&pICommand ); // ppCommand

    *ppICommand_out = pICommand;

    #include <windows.h> //
    #include <ole2ver.h> // OLE2.0 build version
    #include <cguid.h> // GUID_NULL
    #include <stdio.h> // vsnprintf, etc.
    #include <stddef.h> // offsetof
    #include <stdarg.h> // va_arg
    #include <time.h> // time
    #include <assert.h> // assert
    #include <conio.h> // _getch()

    // OLE DB headers
    #include <oledb.h>
    #include <oledberr.h>


    // -----------------------------------------
    // Queries
    // -----------------------------------------

    #define TEST_TABLE "Extra7"
    #define LTEST_TABLE L"Extra7"

    #define CREATE_TABLE_QUERY L"CREATE TABLE " LTEST_TABLE L" (a SMALLINT, b VARCHAR(20))"

    #define INSERT_ROW_QUERY L"INSERT INTO " LTEST_TABLE L" VALUES (777, 'Good number')"
    #define DROP_TABLE_QUERY L"DROP TABLE " LTEST_TABLE



    one step to run commenad
    hr = pICommandText->SetCommandText( DBGUID_DBSQL, CREATE_TABLE_QUERY );
    if (FAILED(hr))
    {
    DUMP_ERROR_LINENUMBER();
    DumpErrorHResult( hr, "SetCommandText");
    goto error;
    }
    hr = pICommand->Execute( NULL, IID_IRowset, NULL, &cRowsAffected, (IUnknown**)&pIRowset );

    pICommandText->Release();
    pICommandText = NULL;
    pICommand->Release();
    pICommand = NULL;
    CoFreeUnusedLibraries();


    OLE DB Provider for MySQL (By Todd Smith)
    strConnection = _T("Provider=MySQLProv;Data Source=test");
    Where test is the name of MySQL database. Also, you can replace the name of the database by the following connection string: server=localhost;DB=test.

  4. #4
    Join Date
    Dec 2004
    Posts
    6

    Re: MySQL Connectivity

    see example in windows NT and vc++ 6.0 In Linux there is no com and dll so sorry....

    Quote Originally Posted by anji_m8
    OleInitialize( NULL );

    OleUninitialize();

    IDBInitialize* pIDBInitialize = NULL;
    IDBCreateCommand* pIDBCreateCommand = NULL;
    ICommand* pICommand = NULL;
    ICommandText* pICommandText = NULL;


    IDBInitialize* pIDBInit = NULL;
    IDBProperties* pIDBProperties = NULL;
    DBPROPSET dbPropSet[1];
    DBPROP dbProp[1];


    hr = CoCreateInstance( CLSID_MySqlProv,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IDBInitialize,
    (void **)&pIDBInit );


    VariantInit(&(dbProp[0].vValue));


    dbPropSet[0].rgProperties = &dbProp[0];
    dbPropSet[0].cProperties = 1;
    dbPropSet[0].guidPropertySet = DBPROPSET_DBINIT;
    dbProp[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
    dbProp[0].dwOptions = DBPROPOPTIONS_REQUIRED;
    dbProp[0].colid = DB_NULLID;
    V_VT(&(dbProp[0].vValue)) = VT_BSTR;
    V_BSTR(&(dbProp[0].vValue)) = SysAllocString( DEF_MYSQL_CONN );

    hr = pIDBInit->QueryInterface( IID_IDBProperties, (void**)&pIDBProperties);
    hr = pIDBInit->Initialize();

    *ppIDBInitialize_out = pIDBInit;

    IDBCreateSession* pIDBCreateSession;
    IDBCreateCommand* pIDBCreateCommand;


    hr = pIDBInitialize->QueryInterface( IID_IDBCreateSession, (void**)&pIDBCreateSession);
    hr = pIDBCreateSession->CreateSession( NULL, IID_IDBCreateCommand, (IUnknown**)&pIDBCreateCommand );
    *ppIDBCreateCommand_out = pIDBCreateCommand;

    ICommand* pICommand = NULL;
    hr = pIDBCreateCommand->CreateCommand (NULL, // pUnkOuter - we are not aggregating
    IID_ICommand, // riid - interface we want on the command object
    (IUnknown**)&pICommand ); // ppCommand

    *ppICommand_out = pICommand;

    #include <windows.h> //
    #include <ole2ver.h> // OLE2.0 build version
    #include <cguid.h> // GUID_NULL
    #include <stdio.h> // vsnprintf, etc.
    #include <stddef.h> // offsetof
    #include <stdarg.h> // va_arg
    #include <time.h> // time
    #include <assert.h> // assert
    #include <conio.h> // _getch()

    // OLE DB headers
    #include <oledb.h>
    #include <oledberr.h>


    // -----------------------------------------
    // Queries
    // -----------------------------------------

    #define TEST_TABLE "Extra7"
    #define LTEST_TABLE L"Extra7"

    #define CREATE_TABLE_QUERY L"CREATE TABLE " LTEST_TABLE L" (a SMALLINT, b VARCHAR(20))"

    #define INSERT_ROW_QUERY L"INSERT INTO " LTEST_TABLE L" VALUES (777, 'Good number')"
    #define DROP_TABLE_QUERY L"DROP TABLE " LTEST_TABLE



    one step to run commenad
    hr = pICommandText->SetCommandText( DBGUID_DBSQL, CREATE_TABLE_QUERY );
    if (FAILED(hr))
    {
    DUMP_ERROR_LINENUMBER();
    DumpErrorHResult( hr, "SetCommandText");
    goto error;
    }
    hr = pICommand->Execute( NULL, IID_IRowset, NULL, &cRowsAffected, (IUnknown**)&pIRowset );

    pICommandText->Release();
    pICommandText = NULL;
    pICommand->Release();
    pICommand = NULL;
    CoFreeUnusedLibraries();


    OLE DB Provider for MySQL (By Todd Smith)
    strConnection = _T("Provider=MySQLProv;Data Source=test");
    Where test is the name of MySQL database. Also, you can replace the name of the database by the following connection string: server=localhost;DB=test.

  5. #5
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: MySQL Connectivity

    Quote Originally Posted by sharanu
    Hai friends,
    I have installed MySQL server 4.0.12 to my system on linux platform. We are using C program to connect to the MySQL database. Can the C code be implemented from C++ without and problems ?
    If yes do we have to make some changes ?
    Please reply me.
    I did not try this with MySQL
    but in general I believe you'll be able make the old code work without alot of changes..
    Check this FAQ :
    http://www.research.att.com/~bs/bs_faq.html#C-is-subset
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured