Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make Mysql dabase connection in multiple thread?
Qt 6.11 is out! See what's new in the release blog

How to make Mysql dabase connection in multiple thread?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    djkim87
    wrote on last edited by
    #1

    Hello. Im a beginner so it would be stupid question let me say sorry first.

    Background:
    I have to make multiple db connection. One thread works regulary searching table to show. Main thread works searching table if user requests. also each thread search table in different database. (ip, port, user, password are same)

    Problem:
    Now Im trying to make multiple connection. However, it doesnt work properly. if I use addDatabase function like QSqlDatabase db2 = QSqlDatabase::addDatabase("QMYSQL", "Other"); db is valid but I got error message. The message said QSqlQuery::exec: database not open.

    my develope environment:
    QT5.0.2, mingw4.7, windows8

    code:

    thread:
    @
    void dThread::run()
    {
    qDebug() << " database Thread Start";

    sleep(2);
    db_mutex.lock();
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "Other");
    //db = QSqlDatabase::database("first");
    qDebug() << db.isValid();
    
    db.setHostName("1.1.1.1");
    db.setPort(3306);
    db.setDatabaseName("a");
    db.setUserName("myid");
    db.setPassword("mypass");
    
    if(!db.open())
    {
        qDebug() << db.lastError().text();
    }
    db_mutex.unlock();
    
    db_mutex.lock();
    
    QSqlQuery query;
    query.exec&#40;"SELECT * FROM info"&#41;;
    while(query.next())
        qDebug() << query.value(0).toString();
    
    db.close();
    db_mutex.unlock();
    

    }
    @

    main:
    @
    int main(int argc, char *argv[])
    {

    QCoreApplication a(argc, argv);
    
    dThread dbrun;
    dbrun.start();
    
    db_mutex.lock();
    QSqlDatabase db2 = QSqlDatabase::addDatabase("QMYSQL");
    db2.setHostName("1.1.1.1");
    db2.setPort(3306);
    db2.setDatabaseName("b");
    db2.setUserName("myid");
    db2.setPassword("mypass");
    
    if(!db2.open())
    {
        qDebug() << db2.lastError().text();
    }
    
    QSqlQuery query2;
    query2.exec&#40;"SELECT * FROM info2"&#41;;
    
    while(query2.next())
    {
        qDebug() << query2.value(1).toString();
    }
    
    db2.close();
    
    db_mutex.unlock();
    
    return a.exec&#40;&#41;;
    

    }
    @

    if you have any idea, please give me advice.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      djkim87
      wrote on last edited by
      #2

      Oh my god... very simple mistake - - . I changed QsqlQuery query(db); and it works...

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved