Hello. Please tell me how to solve the problem, suffered for a long time, but nothing comes out. It is necessary to work simultaneously with two types of databases. How can this be implemented? Individually, everything works, but does not want it together. The console displays:

("QSQLITE", "QMYSQL3", "QMYSQL") ("QSQLITE", "QMYSQL3", "QMYSQL") QSqlQuery::exec: database not open false QSqlQuery::exec: database not open bad 

Search did not give the desired results, found it:

QSqlDatabase defaultDB = QSqlDatabase :: database (); QSqlDatabase firstDB = QSqlDatabase :: database ("first"); QSqlDatabase secondDB = QSqlDatabase :: database ("second");

But how to apply to the project I do not understand. I would be grateful for the help.

 // mysql_connect.h #ifndef MYSQL_CONNECT #define MYSQL_CONNECT #endif // MYSQL_CONNECT static bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "dbsql"); db.setDatabaseName("job_db"); db.setUserName("root"); db.setHostName("localhost"); db.setPassword("pass"); qDebug() << db.drivers(); if (!db.open()) { qDebug() << "Cannot open database: " << db.lastError(); return false; } return true; } static bool createConnectionMYSQLite(QString path) { QSqlDatabase dbsqlite = QSqlDatabase::addDatabase("QSQLITE", "dbsqlite"); dbsqlite.setDatabaseName(path); qDebug() << dbsqlite.drivers(); if (!dbsqlite.open()) { qDebug() << "Cannot open database: " << dbsqlite.lastError(); return false; } return true; } // extract_mail.cpp QString create_sql_add (QString sql_add); QString create_sql_add_2 (QString name, QString value); void extract_mail_is_file (QString files); QString extract_is_thunderbird (QString path); void extract_mail () { createConnection(); QString files; files = "global-messages-db.sqlite"; extract_is_thunderbird (files); } QString extract_is_thunderbird (QString path) { createConnectionMYSQLite (path); QSqlQuery query; QTextCodec *codec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForTr(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForLocale(codec); qDebug() << (query.exec("SELECT name, value FROM contacts, identities WHERE contacts.id = identities.contactID;")); // Считываем данные из базы QSqlRecord rec = query.record(); int nNumber = 0; QString strName, buf; QString strValue; buf = create_sql_add_2("strName", "strValue"); cout << buf.toStdString() << endl; while (query.next()){ strName = query.value(rec.indexOf("name")).toString(); strValue = query.value(rec.indexOf("value")).toString(); if (strName != strValue) { create_sql_add_2(strName, strValue); } else { strName = ""; create_sql_add_2(strName, strValue); } } } QString create_sql_add_2 (QString name, QString value) { // создадим запрос SQL для добавления адреса в БД QString x; QSqlQuery query; x = "INSERT INTO addressbook (name, email) " "VALUES ('"; x += name; x += "', '"; x += value; x += "');"; if (query.exec(x)){cout << "OK" << endl;} else {cout << "bad" << endl;} return x; } 
  • And what is this base in local variables? - Evgeny Shmidt
  • if only one database is used, then there is no problem, everything works. did as I saw options. - shaman888

1 answer 1

Pay attention to the QSqlQuery constructors :

 QSqlQuery(QSqlResult *result) QSqlQuery(const QString &query = QString(), QSqlDatabase db = QSqlDatabase()) QSqlQuery(QSqlDatabase db) QSqlQuery(const QSqlQuery &other) 

In your case: doing so is a QSqlQuery query; - you call the second constructor from the list with the default parameters, and you need to specify with which connection it will work (that is, call the third constructor) when creating each QSqlQuery object:

 QSqlQuery query_sqlite(QSqlDatabase::database("dbsqlite")); QSqlQuery query_mysql(QSqlDatabase::database("dbsql")); 
  • Thank you. In general, I assumed something similar, but I did not find a way to implement it. - shaman888