I am using this SELECT name FROM sqlite_master WHERE type = "table" AND name = 'create_organization';
to check if the table named create_organization exists and it works.However i would like to use qt to find that out.I am trying
QSqlQuery query;
QString tableName = "employee_datastores";
QString sqlQuery = QString("SELECT name FROM sqlite_master WHERE type =:table AND name = %1 ").arg(tableName);
query.prepare(sqlQuery);
query.bindValue(":table", "table");
query.exec();
int fieldNo = query.record().indexOf("employee_datastore_name");
while (query.next()) {
QString employee_ds_name = query.value(fieldNo).toString();
qDebug() << "Table Name" << employee_ds_name ;
}
but it does not work.
Does QSqlDatabase::tables() give you what you want? For example you could try:
if ( database.tables().contains( QLatin1String("employee_datastores") ) {
...
}
You could fix it like this:
QString sqlQuery = QString("SELECT name FROM sqlite_master WHERE type =:table AND name = '%1' ").arg(tableName);
query.prepare(sqlQuery);
query.bindValue(":table", "table");
query.exec();
But why mix bind values and "dumb" string replacement?
QString sqlQuery = QString("SELECT name FROM sqlite_master WHERE type =:table AND name = :tablename ");
query.prepare(sqlQuery);
query.bindValue(":table", "table");
query.bindValue(":tablename", tableName);
query.exec();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With