Important: Please read the Qt Code of Conduct -

Get values from SQLite row using last_insert_rowid()

  • Hi,
    I need to get the values from a query using the row id I tell the query to use, however, I can't seem to know how to call the row id in said query.
    Is this a syntax problem?
    Here's an example of my code:

    QSqlQuery query;
        query.exec("SELECT last_insert_rowid()");
        QString sLastId = query.lastInsertId().toString();
        query.exec("SELECT fPrecioUnit, fLitros, fImporte, rowid WHERE rowid=\'" + sLastId + "\'");

  • HI and welcome to devnet,

    usually a SELECT statement is in the form SELECT <field_list> FROM <table_name> WHERE <conditions>; in your queries there's no FROM <tablename>.

    BTW, QSqlQuery::exec() returns a boolean and you can check the errors with QSqlQuery::lastError()

    bool result = query.exec(...);
    if (!result) {
        qDebug() << "Error: " << query.lastError().text();

Log in to reply