Hi,

I am using
Qt Code:
  1. QSqlDatabase QSqlDatabase::addDatabase ( QSqlDriver * driver, const QString & connectionName = QLatin1String( defaultConnection ) )
To copy to clipboard, switch view to plain text mode 

http://doc.qt.nokia.com/4.7/qsqldata...#addDatabase-2

to connect to a mysql Embedded database (using my.cnf), here is the code:

Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <QtSql>
  3.  
  4. #include "QMYSQLDriver"
  5. #include <mysql.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. //QCoreApplication a(argc, argv);
  10. {
  11. MYSQL *mysql;
  12.  
  13. static char *server_options[] = \
  14. { "mysql_test", "--defaults-file=/home/cquiros/temp/mysql/my.cnf", NULL };
  15. int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
  16.  
  17. static char *server_groups[] = { "embedded", NULL };
  18.  
  19. qDebug() << "Loading embedded";
  20. mysql_library_init(num_elements, server_options, server_groups);
  21. mysql = mysql_init(NULL);
  22. mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "embedded");
  23. mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
  24.  
  25. mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);
  26.  
  27.  
  28. //mydb = QSqlDatabase::addDatabase("QMYSQLE","mydb"); //Add the database connector to MySQL
  29.  
  30. QMYSQLDriver *drv = new QMYSQLDriver(mysql);
  31.  
  32. mydb = QSqlDatabase::addDatabase(drv,"connection1"); //Add the database connector to MySQL
  33. qDebug() << "Embedded driver added";
  34.  
  35. mydb.setDatabaseName("test");
  36.  
  37. if (!mydb.open()) //Try to opens the database
  38. {
  39. qDebug() << "Error while opening the database";
  40. }
  41. else
  42. {
  43. qDebug() << "DB test opened";
  44. QSqlQuery tables(mydb);
  45. QString sql;
  46. sql = "SELECT count(*) FROM system";
  47. if (tables.exec(sql))
  48. {
  49. tables.first();
  50. qDebug() << "Total records is: " << tables.value(0).toString() << " ok.";
  51. }
  52. else
  53. {
  54. qDebug() << tables.lastError().databaseText();
  55. }
  56.  
  57. }
  58. }
  59. qDebug() << "Closing DB";
  60. QSqlDatabase::removeDatabase("connection1"); // correct
  61. qDebug() << "DB closed";
  62.  
  63. //return a.exec();
  64. qDebug() << "En of program....";
  65. }
To copy to clipboard, switch view to plain text mode 

The code works however, there is a segmentation fault when the program finished. If I trace the error it happens is the QMYSQLDriver (from QT sources) when the driver closes:

Qt Code:
  1. void QMYSQLDriver::close()
  2. {
  3. if (isOpen()) {
  4. #ifndef QT_NO_THREAD
  5. mysql_thread_end();
  6. #endif
  7. mysql_close(d->mysql); //Here is the failure
  8. d->mysql = NULL;
  9. setOpen(false);
  10. setOpenError(false);
  11. }
  12. }
To copy to clipboard, switch view to plain text mode 

Any idea what is wrong or if this is not the way to connect using the embedded driver I would appreciate some help.

Thanks,
Carlos.