c++mysqlqtqtsql

Can't check if a table exist in QT MYSQL


I'm trying to check if a table exist in a schema for QMYSQL inside QT framework. I have connected the MySQL server and it can create a table, but NOT check if a table exist.

This is the code for checking if a table exist

query.exec("CREATE TABLE " + table_name + "(ID BIGINT PRIMARY KEY)");
QStringList tables = this->qSqlDatabase.tables();
qDebug() << "Table name: " + table_name;
for(int i = 0; i < tables.length(); i++)
    qDebug() << tables[i];
qDebug() << tables.length();
if(tables.contains(table_name))

The if-statement does not run and the output is:

"Table name: table0"
 0

In this case table_name = "table0". But why does this happening?


Solution

  • try this line:

    query.exec("CREATE TABLE " + table_name + " (ID BIGINT, PRIMARY KEY (ID));");