int CreateDB(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("mydb"); db.setUserName("myuser"); db.setPassword("mypassword"); db.setHostName("192.168.1.2"); if( !db.open() ) qDebug() << db.lastError(); QString conn_id; QSqlQuery query(QSqlDatabase::database("mydb")); if(!query.exec("select CONNECTION_ID()")){ QMessageBox::critical(0, QObject::tr("SQL Error"), query.lastError().text()); return 1; } query.first(); conn_id=query.value(0).toString(); query.clear(); qDebug() << conn_id; if(!query.exec("SHOW PROCESSLIST")){ QMessageBox::critical(0, QObject::tr("SQL Error"), query.lastError().text()); return 1; } while (query.next()) { qDebug() << query.value(0).toString(); if (query.value(0).toString() == conn_id) operHost=query.value(2).toString(); } query.clear(); return 0;}int main(int argc, char *argv[]){ QApplication app(argc, argv); if (CreateDB()!=0) return 0; qDebug() << operHost; return 0;}
C++ (Qt)int CreateDB(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("mydb"); db.setUserName("myuser"); db.setPassword("mypassword"); db.setHostName("192.168.1.2"); if( !db.open() ) qDebug() << db.lastError(); QString conn_id; QSqlQuery query(QSqlDatabase::database("daninet")); if(!query.exec("select CONNECTION_ID()")){ QMessageBox::critical(0, QObject::tr("SQL Error"), query.lastError().text()); return 1; } query.first(); conn_id=query.value(0).toString(); query.clear(); qDebug() << conn_id; QSqlQuery q(QSqlDatabase::database("daninet")); if(!q.exec("SHOW PROCESSLIST")){ QMessageBox::critical(0, QObject::tr("SQL Error"), q.lastError().text()); return 1; } while (q.next()) { qDebug() << q.value(0).toString(); if (q.value(0).toString() == conn_id) operHost=q.value(2).toString(); } query.clear(); return 0;}