How do I find the largest primary key in a model?
-
I need to insert a record doing
model->insertRecord(-1, sqlRecord);
but to insert, I need to specify a new and large enough primary key. How can I obtain the largest primary key in a model??
Also, after inserting how can I make that row the current in a tableView?Regards,
Juan -
@jdent said in How do I find the largest primary key in a model?:
How do I get the value?
By reading documentation: https://doc.qt.io/qt-6/qsqlquery.html#value
There is also an example:QSqlQuery query("SELECT country FROM artist"); while (query.next()) { QString country = query.value(0).toString(); doSomething(country); }
-
@jdent said in How do I find the largest primary key in a model?:
I need to specify a new and large enough primary key.
Why? Primary keys are usually auto-increment and you do not specify them explicitly.
-
@jdent Well, then you will have to query it using max(): https://www.w3schools.com/sql/sql_min_max.asp
But keep in mind that between querying the max primary key and inserting the record another transaction could increase the primary key. You should do the querying and inserting in one transaction. -
@jdent said in How do I find the largest primary key in a model?:
How do I get the value?
By reading documentation: https://doc.qt.io/qt-6/qsqlquery.html#value
There is also an example:QSqlQuery query("SELECT country FROM artist"); while (query.next()) { QString country = query.value(0).toString(); doSomething(country); }
-