MYSQL *mysql;// ... тут соединение с БД QMYSQLDriver *drv = new QMYSQLDriver(mysql); QSqlDatabase mydb = QSqlDatabase::addDatabase( drv, "connection1" );//... тут простой запрос, отрабатывает как надо// и приложение ломается if (!mysql) return; QSqlDatabase::removeDatabase( "connection1" ); mysql_close(mysql); mysql_library_end();
#include "mainwindow.h"#include <QtDebug>#include <QtSql/QSqlDatabase>#include <QtSql/QSqlError>#include <QtSql/QSqlDriver>#include <QtSql/QtSql>#include <QtSql/QSqlQuery>#include <QtSql/QSqlRecord>#include <my_config.h>#include <my_global.h>#include <mysql.h>#include "../src/sql/drivers/mysql/qsql_mysql.cpp"MYSQL *mysql;MYSQL_RES *results;MYSQL_ROW record;static char *server_options[]= { "mysql_test", "--defaults-file=my.ini", NULL };int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;static char *server_groups[] = { "client", "mysqld" "mysql", NULL };MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ qDebug() << "Loading embedded, num_elements"<< num_elements << ".."; int mli = mysql_library_init(num_elements, server_options, server_groups); qDebug() << "init code " << mli; mysql = mysql_init(NULL); mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "client"); mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); if (mysql != mysql_real_connect( mysql, NULL,NULL,NULL, "mysql", 0, NULL, 0 )){ qDebug() << "Warning! mysql_real_connect faulted!"; return; }//// Этот код отрабатывает без проблем, но без привязок к классам Qt//// mysql_query(mysql, "SELECT * FROM `user`");// results = mysql_store_result(mysql);//// while((record = mysql_fetch_row(results))) {// qDebug("%s - %s \n", record[0], record[1]);// }//// mysql_free_result(results);// mysql_close(mysql);// mysql_library_end(); connect(this, SIGNAL(destroyed()), this, SLOT(releaseMysql())); QMYSQLDriver *drv = new QMYSQLDriver(mysql); QSqlDatabase mydb = QSqlDatabase::addDatabase( drv, "connection1" ); qDebug() << "Embedded driver added"; mydb.setDatabaseName("mysql"); if ( !mydb.isValid() ){ qDebug() << "\n\tERR!!! Database driver use fails"; return; } qDebug() << "\n\tOK... Database driver accepted"; if ( mydb.isOpen() ){ //Try to opens the database qDebug() << "The database is already opened"; }else{ qDebug() << "Try to opens the database"; mydb.open(); } if (mydb.isOpen()){ QSqlQuery query; QString queryText = "SELECT * FROM `user`"; QSqlRecord r; query = mydb.exec( queryText ); while( query.next() ){ r = query.record(); qDebug("%s - %s \n", r.value(0).toString(), r.value(1).toString()); } qDebug() << "The database is accessable"; }else qDebug() << "Error while opening the database";}void MainWindow::releaseMysql(){ if (!mysql) return; // После следующей инструкции возникает эксепшн // QSqlDatabase::removeDatabase( "connection1" ); mysql_close(mysql); mysql_library_end(); mysql = 0;}MainWindow::~MainWindow(){}
QT += core gui sqlgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = untitledTEMPLATE = appINCLUDEPATH+="C:/mysql/include/"INCLUDEPATH+=$$[QT_INSTALL_PREFIX]/src/sql/kernel/INCLUDEPATH+=$$[QT_INSTALL_PREFIX]/include/QtSql/5.4.2/INCLUDEPATH+=$$[QT_INSTALL_PREFIX]/include/QtCore/5.4.2/QtCoreSOURCES += \ $$[QT_INSTALL_PREFIX]/src/sql/drivers/mysql/qsql_mysql.cpp\ main.cpp\ mainwindow.cpp\HEADERS += mainwindow.hwin32:LIBS+="-L C:/mysql/libmysqld/Debug"
C++ (Qt)QSqlDatabase mydb = QSqlDatabase::addDatabase( "MYSQL", "connection1" );
//... QSqlDatabase mydb;{ QMYSQLDriver *drv = new QMYSQLDriver(mysql); //QSqlDatabase mydb = QSqlDatabase::addDatabase( drv, "connection1" ); qDebug() << "Embedded driver added"; mydb.setDatabaseName("mysql"); if ( !mydb.isValid() ){ qDebug() << "\n\tERR!!! Database driver use fails"; return; } qDebug() << "\n\tOK... Database driver accepted"; if ( mydb.isOpen() ){ //Try to opens the database qDebug() << "The database is already opened"; }else{ qDebug() << "Try to opens the database"; mydb.open(); }} if (mydb.isOpen()){//... query.clear(); releaseMysql(); mydb.close(); qDebug() << "The database is accessable"; }else qDebug() << "Error while opening the database";
C++ (Qt)QSqlDatabase::removeDatabase("connection1");