mtangoo / wxDatabase

wxWidgets library for connecting to major relational database
http://mtangoo.github.io/database/index.html
36 stars 16 forks source link

wxWidgets 3.1.5 and wxDatabase and ODBC #27

Closed Melandr closed 2 years ago

Melandr commented 2 years ago

Good day! Tell me how to use ODBC correctly with MS SQL server? I am using wxWidgets 3.1.5 and wxDatabase. The wxDatabaseApp.cpp example builds fine. I am trying to change it to work with ODBC, but looking at the source code in the CodeBlocks project, I see that the wxUSE_DATABASE_ODBC preprocessor directive is not set. And the code is copied without ODBC support. Also how to properly link the wxDatabase library to the new project? Sorry for the silly questions, not strong enough in C ++ yet. cmake codeblocks

I am trying to connect to MS SQL server using the following connection string pDatabase = GetDatabase ("[ODBC] \ nConnection = DRIVER = SQL Server; SERVER = 10.9.1.50; DATABASE = apacs24ms; UID = user; PWD = password;");

manyleaves commented 2 years ago

Hi It's been a few years since I did the TDS support in wxDatabase. I seem to remember TDS direct using FreeTDS working fine until an upgrade and then I resorted to accessing TDS via ODBC.

Please find below the content of the test.conf file I used last for testing. You'll notice that the only one uncommented is the TDS via ODBC

;SQLite3 via ODBC ;[ODBC] ;Connection=DRIVER=SQLite3 ODBC Driver;Database=C:\Documents and Settings\Andrew\My Documents\wxWidgets\samples\database\tests\test.sqlite; ;DbType=SQLITE

;Access via ODBC ;[ODBC] ;Connection=DRIVER={microsoft access driver (*.mdb)};dbq=C:\Documents and Settings\Andrew\My Documents\Downloads\NewDBtest\NewDBtest\db\db1.mdb;\nDSN=\nDbType=TDS\n"); ;DbType=TDS

;SQLite3 direct ;[SQLite] ;database=C:\Users\Andrew Lawrance\Documents\wxWidgets\samples\database\tests\test.sqlite

;[PostgreSQL] ;library_location=C:\Program Files\PostgreSQL\8.3\bin ;server=127.0.0.1 ;database=test_databaselayer ;user=user ;password=password ;port=5432

;[MySQL] ;library_location=C:\Program Files\MySQL\MySQL Server 5.1\bin ;server=127.0.0.1 ;database=test_databaselayer ;user=test ;password=test

;TDS via ODBC ;[ODBC] ;Connection=DRIVER=SQL Server;SERVER=manyleaves\sqlexpress17;TRUSTED_CONNECTION=Yes;DATABASE=Test; connection=DRIVER={SQL Server Native Client 11.0};MARS_CONNECTION=Yes;SERVER=manyleaves\sqlexpress17;TRUSTED_CONNECTION=Yes;DATABASE=test;DSN= DbType=TDS

;TDS direct (see freetds.conf) [TDS] ; can also setenv(FREETDS) instead of freetds=... freetds=C:\wxDev\freetds-1.00.24\freetds.conf server=manyleaves_sqlexpress ;server=127.0.0.1\sqlexpress17 database=test ;;version=7.3 ;; user and password are necessary for trusted logins for version 7.2 and later ;;user=manyleaves\user ;;password=test

I hope this helps

Andrew

Melandr commented 2 years ago

Do I need to build a TDS-enabled library to use ODBC? Where should the wxUSE_DATABASE_ODBC preprocessor directive be uncommented? In file C: \ Library \ wxDatabase \ src \ database \ odbc \ odbc_database.cpp this preprocessor directive is used for conditional compilation 1

Melandr commented 2 years ago

Here is the result of running the application with an ODBC connection string. In the file c: \ Library \ wxDatabase \ include \ wx \ database \ setup.h specified the preprocessor directive #define wxUSE_DATABASE_ODBC 1. But I don't see this file in the project structure in CodeBlocks 2

Melandr commented 2 years ago

It turned out to build the application using the library. But there are problems when passing the connection string from the config file. Since I did a little bit with the implementation of the ODBC API, there are two ways to connect the databases. one using a DSN, the second is a direct connection to the database driver using a username and password. Is there a direct connection in the library using a wrapper over the SQLDriverConnect function 3 The dbType parameter is requested. what should i specify in the config file? `#if wxUSE_DATABASE_ODBC wxDatabase wxDatabase::GetOdbcDatabase(wxConfigBase& config, wxString err) { if (!config.HasGroup("ODBC")) { if (err) err->Append("/ODBC not defined"); return NULL; } config.SetPath("ODBC");

if (!wxOdbcDatabase::IsAvailable())
{
    if (err) err->Append("ODBC database backend is not available");
    return NULL;
}

wxString dbType;
if (!config.Read("DbType", &dbType))
{
    if (err) err->Append("/ODBC/DbType not defined");
    return NULL;
}

wxOdbcDatabase* pDatabase = new wxOdbcDatabase();
pDatabase->m_typeName = dbType;

wxString connection;
wxString user;
wxString password;
if (config.Read("Connection", &connection))
{
    pDatabase->Open(connection);
}
else
{
    wxString DSN;
    if (!config.Read("DSN", &DSN, wxEmptyString))
    {
        if (err) err->Append("/ODBC/DSN not defined");
        return NULL;
    }
    if (config.Read("user", &user, wxEmptyString))
    {
        config.Read("password", &password, wxEmptyString);
        pDatabase->Open(DSN, user, password);
    }
    else
    {
        pDatabase->Open(DSN, wxEmptyString, wxEmptyString);
    }
}

wxString libraryPath;
if (config.Read("library_path", &libraryPath))
{
    pDatabase->m_libraryPath = libraryPath;
}

return pDatabase; 

}

endif`

manyleaves commented 2 years ago

My apologies, the active string in the config I sent yesterday was for TDS direct and [ODBC] was partially commented out. For TDS via ODBC it should read as follows

;TDS via ODBC [ODBC] Connection=DRIVER={SQL Server Native Client 11.0};MARS_CONNECTION=Yes;SERVER=manyleaves\sqlexpress17;TRUSTED_CONNECTION=Yes;DATABASE=test;DSN= DbType=TDS

NB: The DbType parameter is on a new line as above. This is what tells wxDatabase what type of ODBC Db to expect.

Melandr commented 2 years ago

Specified the following settings in the configuration file [ODBC] Connection = DRIVER = {SQL Server}; SERVER = 192.168.1.10; DATABASE = apacs24ms; UID = user; PWD = password; DbType = TDS The program connected to the MS SQL server, a table was created and displayed in the console test

thanks