Qt - Check if table is empty in the database

Asked

Viewed 372 times

3

I have an application with a database connection, when initializing the application I need to check if there is any record in the table, if a user should not be registered, when I run the query SELECT EXISTS (SELECT id FROM va_admins) directly in the database is returned 0 if there is no record, if there is no return 0, however in the application, it is always returned 0, I am making the query this way:

bool Admin::issetAdm()
{

    QSqlDatabase db = QSqlDatabase::database();

    if(!db.isOpen()){
        QMessageBox::critical(nullptr, "Falha ao verificar adm", "nenhuma conexão com o banco de dados encontrada");
        qCritical() << "Falha ao verificar: " << db.lastError().text();
        return false;
    }

    QSqlQuery q("SELECT EXISTS (SELECT id FROM va_admins)", db);

    if(!q.exec()){
        qCritical() << "Falha ao realizar a consulta: " << q.lastError().text();
        exit(-1);
    }

    //Editado
    while(q.next()){
        qDebug() << q.record().value(q.record().indexOf("id")).toInt();
    }

    q.finish();

    return true;
}

2 answers

4


Tries to change the select, makes a SELECT COUNT(CAMPO) FROM TABELA (Select Count(id) from va_admins no seu caso). This way you can get a result of how many records you have.

1

After carrying out the consultation with QSqlQuery.exec(const QString &query) it is necessary to place the result in a valid record so that the values can be read. You can do this, for example by using the next method().

Another detail to take into account is that in the result of your query there is no column named "id", so the result of the expression q.record().indexOf("id") is -1.

Here is a small example:

QSqlQuery q("SELECT EXISTS (SELECT id FROM va_admins)");

while (q.next())
     qDebug() << q.value(0).toString();        

Here is the translation of an extract from the relevant section of the documentation, for future reference.

bool Qsqlquery::exec(const Qstring & query)

Executes the SQL statement in the query variable. Returns true and sets the state of the query active if its execution has been successful, otherwise returns false. Query statement must use appropriate syntax for the SQL database being consulted (e.g., SQL pattern).

After the query is executed, the query is positioned in a invalid record and must be navigated to a valid record before that data values can be recovered (for example by using next ()).

The original, for those who prefer English.

bool Qsqlquery::exec(const Qstring & query)

Executes the SQL in query. Returns true and sets the query state to active if the query was Successful; otherwise Returns false. The query string must use syntax appropriate for the SQL database being queried (for example, standard SQL).

After the query is executed, the query is positioned on an invalid record and must be navigated to a Valid record before data values can be be retrieved (for example, using next()).

  • Continues to return 0

  • @Samuelives, see the edit. Your query does not return a record with a column called "id". Returns the result of the EXISTS predicate. The result of the expression "q. record(). indexof("id")" will be -1.

  • Who voted negative, could help me change the answer in order to remove the incorrect information? Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.