From d6b7af2244759b78fbab56e3b8f47e3a41f06fbf Mon Sep 17 00:00:00 2001 From: danylo-nevidomiy Date: Sun, 9 Jan 2022 02:53:18 +0300 Subject: [PATCH] The problem of ten was fixed Hooray!!!!! --- PasswordManager/mainwindow.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/PasswordManager/mainwindow.cpp b/PasswordManager/mainwindow.cpp index 0bde7fa..c3ef764 100644 --- a/PasswordManager/mainwindow.cpp +++ b/PasswordManager/mainwindow.cpp @@ -161,12 +161,23 @@ void MainWindow::deleteRecord() int MainWindow::getRecordsCount(){ query->clear(); qDebug() << "query->clear();"; - query->prepare("SELECT MAX(r.id) AS 'number_of_records' FROM records AS 'r';"); + query->prepare("SELECT * FROM records"); query->exec(); + QSqlRecord rec = query->record(); query->next(); - QString count = QString::number(query->value(0).toInt() + 1); - return count.toInt(); + int max = query->value(rec.indexOf("id")).toInt(); + int cur = max; + while(query->next()) + { + cur = query->value(rec.indexOf("id")).toInt(); + if(max < cur) + { + max = cur; + } + } + qDebug() << "max" << max; + return max; } void MainWindow::connectToDatabase(const QString &dbName) { @@ -347,10 +358,10 @@ void MainWindow::addRecordSlot(Record *r) // qDebug() << "id: " << getRecordsCount(); qDebug() << "id here:"; - r->setId(ui->tableViewRecords->model()->rowCount() + 1); +// r->setId(ui->tableViewRecords->model()->rowCount() + 1); // r->setId(ui->tableViewRecords->selectionModel()->selection().indexes().at(0).row()); qDebug() << "id here:"; - + r->setId(getRecordsCount()+1); putRecord(r); qDebug() << "id here:";