Connecting to a MySQL database using ODBC
-
wrote on 28 Jan 2011, 10:52 last edited by
I have always connected to Mysql using QMYSQL driver, but now I need to do it using de MYSQL ODBC driver. I have downloaded the ODBC driver por MYSQL and I have installed.
I have created a DSN using Administrative Tools (Windows 7) and when I test the connection, it is successful. But I am doing something wrong in my Qt Application.
The DNS name I have created is Mysql-DS and my code in QT Application is this.
@
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("agp-dbserver01");
db.setDatabaseName("xcdr");
db.setPassword("xcdr");
db.setUserName("xcdr");if (!db.open())
{
std::cout << "Error opening database" << std::endl;
return -1;
}
else
{
return 0;
}
@When I run the application, I take the following message:
@
QSqlQuery::prepare: database not open "QODBCResult::exec: No statement handle available"
Error: "[Microsoft][ODBC
Driver Manager] Data source name not found and no default driver specified"
@To be honest I dont know if I need to create a DNS or not, and I dont know if in Database Name I need to put the scheme name of the DSN Name...
What am I missing?
Thanks in advance
[EDIT: code formatting, please use @-tags, Volker]
-
wrote on 28 Jan 2011, 12:44 last edited by
Try this:
@
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={MySQL ODBC 5.1 Driver};DATABASE=agp-dbserver01;");
db.setUserName("xcdr");
db.setPassword("xcdr");@ -
wrote on 16 Feb 2011, 22:31 last edited by
Now it works without change any line in code. The only thing I had to do was to configura a DSN called as the database.
Thanks a lot.