Home > Mobile >  QT opened the MySQL database, takes 1-10 seconds, your bosses have any good solution?
QT opened the MySQL database, takes 1-10 seconds, your bosses have any good solution?

Time:09-17



My code is as follows, I am connectDB this sentence output before and after the current time, about time consuming of 1 to 10 seconds, one second can also accept, but 10 seconds takes too long, can't stand, do you have any good solution?


Bool DataBaseUnit: : connectDB (QString strHostName, QString strDataBase, int nPort, QString strUserName, QString strPassword)
{
The dataBase=QSqlDatabase: : addDatabase (" QMYSQL ");
DataBase. SetHostName (strHostName);
DataBase. SetDatabaseName (strDataBase);
DataBase. SetPort (nPort);
DataBase. SetUserName (strUserName);
The dataBase. The setPassword (strPassword);
Bool bRet=false;
BRet=dataBase. The open ();
If (bRet==false)
{
QDebug () & lt; <" The database connect error: "& lt; }
Return bRet.
}

CodePudding user response:

 QSqlDatabase DataBaseUnit: : connectDB (QString strHostName, QString strDataBase, int nPort, QString strUserName, QString strPassword) 
{
//assume that your connection name connectionName is strDataBase + thread ID
If (QSqlDatabase: : database (connectionName). IsOpen ()) {
Return QSqlDatabase: : database (connectionName);
}

QSqlDatabase db=QSqlDatabase: : addDatabase (" QMYSQL ", connectionName);
The setHostName (strHostName);
Db. SetDatabaseName (strDataBase);
The setPort (nPort);
The setUserName (strUserName);
The setPassword (strPassword);
The open ();
Return the db;
}

CodePudding user response:

reference 1st floor donwmufromdying response:
 QSqlDatabase DataBaseUnit: : connectDB (QString strHostName, QString strDataBase, int nPort, QString strUserName, QString strPassword) 
{
//assume that your connection name connectionName is strDataBase + thread ID
If (QSqlDatabase: : database (connectionName). IsOpen ()) {
Return QSqlDatabase: : database (connectionName);
}

QSqlDatabase db=QSqlDatabase: : addDatabase (" QMYSQL ", connectionName);
The setHostName (strHostName);
Db. SetDatabaseName (strDataBase);
The setPort (nPort);
The setUserName (strUserName);
The setPassword (strPassword);
The open ();
Return the db;
}


Behind me to use to connect the name, but for the first time after refactoring connection time longer, about 2 seconds, the second time in running fast, tens of milliseconds

CodePudding user response:

Slow connection for the first time that estimate or can't, because it is a network connection, three times handshake

CodePudding user response:

Close the firewall, may be a network problem,
Use the MySQL client MySQL. Exe, give it a try?
  •  Tags:  
  • Qt
  • Related