odbc + mysql

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

odbc + mysql

Post by massimopasquali »

ok someone can tell me how to use wxWidgest to make a connectio at a database mysql by odbc? and why this error

4 C:\Dev-Cpp\include\wx\log.h:47, from main.cpp In file included from C:/Dev-Cpp/include/wx/log.h:47, from main.cpp

can you send me a example to a connection witdh mysql?
emarti
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 210
Joined: Sat May 07, 2005 8:24 pm
Location: Eskisehir, TURKEY
Contact:

Post by emarti »

Please, look at the
http://www.wxwidgets.org/wiki/index.php/ODBC
or
/wxWidgets/examples/dbbrowse
- T U R K E Y ?
- I love this country!

WebSites:
http://mebt.sourceforge.net/
http://wxquran.sourceforge.net/
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

someone can to correct this for me please

Code: Select all

// ----------------------------------------------------------------------------
// HEADERS
// ----------------------------------------------------------------------------
#include "log.h"         // #included to enable output of messages only
#include "dbtable.h"
#include "db.h"
#include "string.h"
#include "msgdlg.h"

// ----------------------------------------------------------------------------
// FUNCTION USED FOR HANDLING/DISPLAYING ERRORS
// ----------------------------------------------------------------------------
// Very generic error handling function.
// If a connection to the database is passed in, then we retrieve all the
// database errors for the connection and add them to the displayed message
int HandleError(wxString errmsg, wxDb *pDb=NULL)
{
    // Retrieve all the error message for the errors that occurred
    wxString allErrors;
    if (!pDb == 0)
        // Get the database errors and append them to the error message
        allErrors = wxDbLogExtendedErrorMsg(errmsg.c_str(), pDb, 0, 0);
    else
        allErrors = errmsg;

    // Do whatever you wish with the error message here
    // wxLogDebug() is called inside wxDbLogExtendedErrorMsg() so this
    // console program will show the errors in the console window,
    // but these lines will show the errors in RELEASE builds also
    wxFprintf(stderr, wxT("\n%s\n"), allErrors.c_str());
    fflush(stderr);

    return 1;
}


// ----------------------------------------------------------------------------
// entry point
// ----------------------------------------------------------------------------
int main(int argc, char **argv)
{
//wxDbConnectInf  *DbConnectInf    = NULL;    // DB connection information
struct wxDbConnectInf  DbConnectInf;

wxDb            *db              = NULL;    // Database connection

wxDbTable       *table           = NULL;    // Data table to access
const wxChar     TableName[]     = wxT("clienti"); // Name of database table
const UWORD      numTableColumns = 14;       // Number table columns

wxChar           codice_cliente[15+1];
wxChar           ragione_sociale[45+1];
wxChar           nome[45+1];
wxChar           cognome[45+1];
wxChar           indirizzo[80+1];
wxChar           citta[45+1];
wxChar           provincia[3+1];
wxChar           p_iva[12+1];
wxChar           c_fiscale[16+1];
wxChar           data_nascita[10+1];
wxChar           cellulare[15+1];
wxChar           email[50+1];
wxChar           cap[5+1];
wxChar           codice_azienda[15+1];

wxString         msg;                       // Used for display messages

// -----------------------------------------------------------------------
// DEFINE THE CONNECTION HANDLE FOR THE DATABASE
// -----------------------------------------------------------------------
//DbConnectInf = new wxDbConnectInf(NULL, wxT("prova"), wxT("root"), wxT("pas01@fi"), "");

SQLAllocEnv(&DbConnectInf.Henv);
wxStrcpy(DbConnectInf.Dsn,        "cassa");
wxStrcpy(DbConnectInf.Uid,        "gfh");
wxStrcpy(DbConnectInf.AuthStr,    "dfgh");
//wxStrcpy(DbConnectInf.DefaultDir, "");




// Initialize the ODBC Environment for Database Operations
if (SQLAllocEnv(&DbConnectInf.Henv) != SQL_SUCCESS)
{
    wxMessageBox("A problem occured while trying to get a connection to the datasource","DB CONNECTION ERROR",wxOK | wxICON_EXCLAMATION);
    return 1;
}


// -----------------------------------------------------------------------
// GET A DATABASE CONNECTION
// -----------------------------------------------------------------------

db = wxDbGetConnection(&DbConnectInf);
if (!db)
{
   wxLogError("ERRORE DI CONNESSIONE") ;
}
wxLogMessage("IP21 Connection succeeded !") ;


// -----------------------------------------------------------------------
// DEFINE THE TABLE, AND THE COLUMNS THAT WILL BE ACCESSED
// -----------------------------------------------------------------------
table = new wxDbTable(db, TableName, numTableColumns, wxT(""), !wxDB_QUERY_ONLY);


//
// Bind the columns that you wish to retrieve. Note that there must be
// 'numTableColumns' calls to SetColDefs(), to match the wxDbTable def
//
// Not all columns need to be bound, only columns whose values are to be
// returned back to the client.
//

table->SetColDefs(0, wxT("codice_cliente"), DB_DATA_TYPE_VARCHAR, codice_cliente, SQL_C_WXCHAR, sizeof(codice_cliente), true, true);
table->SetColDefs(1, wxT("ragione_sociale"), DB_DATA_TYPE_VARCHAR, ragione_sociale, SQL_C_WXCHAR, sizeof(ragione_sociale), false, true);
table->SetColDefs(2, wxT("nome"), DB_DATA_TYPE_VARCHAR, nome, SQL_C_WXCHAR, sizeof(nome), true, true);
table->SetColDefs(3, wxT("cognome"), DB_DATA_TYPE_VARCHAR, cognome, SQL_C_WXCHAR, sizeof(cognome), false, true);
table->SetColDefs(4, wxT("indirizzo"), DB_DATA_TYPE_VARCHAR, indirizzo, SQL_C_WXCHAR, sizeof(indirizzo), false, true);
table->SetColDefs(5, wxT("citta"), DB_DATA_TYPE_VARCHAR, citta, SQL_C_WXCHAR, sizeof(citta), false, true);
table->SetColDefs(6, wxT("provincia"), DB_DATA_TYPE_VARCHAR, provincia, SQL_C_WXCHAR, sizeof(provincia), false, true);
table->SetColDefs(7, wxT("p_iva"), DB_DATA_TYPE_VARCHAR, p_iva, SQL_C_WXCHAR, sizeof(p_iva), true, true);
table->SetColDefs(8, wxT("c_fiscale"), DB_DATA_TYPE_VARCHAR, c_fiscale, SQL_C_WXCHAR, sizeof(c_fiscale), true, true);
table->SetColDefs(9, wxT("data_nascita"), DB_DATA_TYPE_VARCHAR, data_nascita, SQL_C_WXCHAR, sizeof(data_nascita), true, true);
table->SetColDefs(10, wxT("cellulare"), DB_DATA_TYPE_VARCHAR, cellulare, SQL_C_WXCHAR, sizeof(cellulare), false, true);
table->SetColDefs(11, wxT("email"), DB_DATA_TYPE_VARCHAR, email, SQL_C_WXCHAR, sizeof(email), false, true);
table->SetColDefs(12, wxT("cap"), DB_DATA_TYPE_VARCHAR, cap, SQL_C_WXCHAR, sizeof(cap), false, true);
table->SetColDefs(13, wxT("codice_azienda"), DB_DATA_TYPE_VARCHAR, codice_azienda, SQL_C_WXCHAR, sizeof(codice_azienda), true, true);




// -----------------------------------------------------------------------
// OPEN THE TABLE FOR ACCESS
// -----------------------------------------------------------------------
if (!table->Open())
{
    return HandleError(wxT("NON SI APRE LA TABELLA: "), table->GetDb());
}


// -----------------------------------------------------------------------
// INSERT A NEW ROW INTO THE TABLE
// -----------------------------------------------------------------------
wxStrcpy(codice_cliente, wxT("000150"));
wxStrcpy(ragione_sociale, wxT("Cavalli Firenze"));
wxStrcpy(nome, wxT("Massimo"));
wxStrcpy(cognome, wxT("Pasquali"));
wxStrcpy(indirizzo, wxT("Via Mascagni, 10"));
wxStrcpy(citta, wxT("lastra a signa"));
wxStrcpy(provincia, wxT("fi"));
wxStrcpy(p_iva, wxT("04793840481"));
wxStrcpy(c_fiscale, wxT("msmpsq71idf612d"));
wxStrcpy(data_nascita, wxT("15/12/1971"));
wxStrcpy(cellulare, wxT("3495388790"));
wxStrcpy(email, wxT("[email protected]"));
wxStrcpy(cap, wxT("50055"));
wxStrcpy(codice_azienda, wxT("1"));


if (!table->Insert())
{
    return HandleError(wxT("ERRORE DI INSERIMENTO: "), table->GetDb());
}

// Must commit the insert to write the data to the DB
table->GetDb()->CommitTrans();


// -----------------------------------------------------------------------
// RETRIEVE ROWS FROM THE TABLE BASED ON SUPPLIED CRITERIA
// -----------------------------------------------------------------------
// Set the WHERE clause to limit the result set to return
// all rows that have a value of 'JULIAN' in the FIRST_NAME
// column of the table.
//table->SetWhereClause(wxT("nome = 'Massimo'"));

// Result set will be sorted in ascending alphabetical
// order on the data in the 'LAST_NAME' column of each row
//table->SetOrderByClause(wxT("nome"));

// No other tables (joins) are used for this query
//table->SetFromClause(wxT(""));

// Instruct the datasource to perform a query based on the
// criteria specified above in the where/orderBy/from clauses.
//if (!table->Query())
//{
//    return HandleError(wxT("QUERY ERROR: "), table->GetDb());
//}

// Loop through all rows matching the query criteria until
// there are no more records to read
//while (table->GetNext())
//{
//   msg.Printf(wxT("Riga  -- Nome : %s Cognome %s"), nome, cognome);
//   //Code to display 'msg' here
//   wxLogMessage(wxT("\n%s\n"), msg.c_str());
//}


// -----------------------------------------------------------------------
// DELETE A ROW FROM THE TABLE
// -----------------------------------------------------------------------
// Select the row which has FIRST_NAME of 'JULIAN' and LAST_NAME
// of 'SMART', then delete the retrieved row
//
//if (!table->DeleteWhere(wxT("nome = 'Massimo' and cognome = 'Pasquali'")))
//{
// HandleError(wxT("DELETION ERROR: "), table->GetDb());
//}

// Must commit the deletion to the database
//table->GetDb()->CommitTrans();


if (table)
{
    delete table;
    table = NULL;
}
if (db)
{
    wxDbFreeConnection(db);
    db = NULL;
}



return 0;
}
becouse this no run
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

massimopasquali,
WHat is happening? Does it give you a compilation error? If it compiles clearly, is it run clearly (i.e. no crush)? If it is then what happen when you runit under debugger?

Also what wx version you are using, OS and compiler?

Thank you.
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

When i running the application this does not give no error, but in mine database it does not happen nothing I do not have record in the table clienti, so i have launch the debug and I have uncovered that when arrives to the logon gives I an error of access violation
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

xp sp2 , Wx 6.10 beta , Mysql driver odbc 3.5.12 , g++ but i don't know the version
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

massimopasquali,
0. Do you have an access to the DB? Is the login credentials OK? Does the user have a DB access rights?
1. Did you try to connect using mySQL standard program? Did it connect successfully?
2. Did you try to use wx sample? Did it work?

Thank you.
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

Yes i have an access to the db
My user ave the access right
I had tested my odbc under windows xp, and it's ok

I have found that the program make a access violaction when try to make a AllocHenv for the environment.

I have try to use the sample but it's a the some thing, when i compiling or lunch the application this is ok no error, but is an illusion in realy the application stopped go out when try to connect at DB, in particulary when AllocHenv.

I say this becouse i have to make a non cacheble connection and whith the debug F7 i see that the application to give me the error of application when to make this :

if (DbConnectionInf.AllocHenv)
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

Thanks for your help

but you have an functionaly example with Windows ODBC Driver 3.5.12 Mysql and WxWidgest?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

massimopasquali,
Could you please check with the MSDN?
You need to put in your code following:

Code: Select all

if( DbConnectionInf.AllocHenv)
{
       // success
}
else
{
      SQLGetDiagRec();
}
Check with MSDN for the parameters o call the function and also how to interpre he error code yopu are receiving?

The link is here.

Thank you.
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

i have put your code in this whay

// -----------------------------------------------------------------------
// DEFINE THE CONNECTION HANDLE FOR THE DATABASE
// -----------------------------------------------------------------------

if( DbConnectInf->AllocHenv())
{
// success
}
else
{
SQLGetDiagRec();
}

DbConnectInf = new wxDbConnectInf(NULL, "cassa", "igrevi", "igrevi06");

if (!DbConnectInf)
{
return HandleError(wxT("ERRORE DB"));
}

risultato = DbConnectInf->GetHenv();

if (risultato == false)
{
return HandleError(wxT("ERRORE DB"));
}


db = wxDbGetConnection(DbConnectInf);


but the compiler give me

394 C:\Programmi\Dev-Cpp\include\sql.h too few arguments to function `SQLRETURN SQLGetDiagRec(SQLSMALLINT, void*, SQLSMALLINT, SQLCHAR*, SQLINTEGER*, SQLCHAR*, SQLSMALLINT, SQLSMALLINT*)'
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

if i write this

if( DbConnectInf.AllocHenv())
{
// success
}
else
{
SQLGetDiagRec();
}

the wxwidgets give me

71 D:\Progetti-Programmi\esercizi\provaDB\provaDB.cpp `AllocHenv' has not been declared

or of i write this


if( DbConnectInf.AllocHenv)
{
// success
}
else
{
SQLGetDiagRec();
}

then say me

71 D:\Progetti-Programmi\esercizi\provaDB\provaDB.cpp `AllocHenv' has not been declared

:cry:
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

massimopasquali,
Did you check MSDN?
I think you need to include "odbcinst.h" in order for SQLGetyDiagRec() to be recognized...

Also you might need "sqlext.h"
Thak you.
massimopasquali
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Sep 11, 2006 7:40 am
Location: lastra a signa (FI) - Italy
Contact:

Post by massimopasquali »

OK thanks to help me, i have resolve width driver odbc 2.5 for windows
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

massimopasquali,
Don't forget to put [SOLVED] in the thread header... ;-)

Thank you.
Post Reply