简体   繁体   中英

How to UPDATE data in database (MS Access .mdb) by using SQL

I want to update data in database (MS Access .mdb) by using SQL in QT Creator C++, but nothing happens.

I tried to google this, but still nothing.

void Chairs::on_pushButton_clicked()
{
    mDatabase = QSqlDatabase::addDatabase("QODBC");
    mDatabase.setDatabaseName(ACCESS);

   if(!mDatabase.open())
   {
       QMessageBox::critical(this, "Error", 
mDatabase.lastError().text());
       return;
   }

   int quantity_of_chairs = 14;

   int value = 1;
    for (int i = 0; i < quantity_of_chairs; i++)
    {
        if(ui->comboBox->currentText() == value)
        {
            QSqlQueryModel *setquery1 = new QSqlQueryModel;
            setquery1->setQuery("UPDATE Chairs SET Status = 'Ordered' 
WHERE number_of_chair = "+value);
            QTableView *tv = new QTableView(this);
            tv->setModel(setquery1);
            ui->tableView->setModel(setquery1);
        }
        value++;
    }
}

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

The answer is quite old, but having had the same problem myself, I found a solution (not necessarily the most optimized one, certainly) that I found on no subject.

This message appears because you are trying to connect more than one on the same connection (the default one)

You just have to change the default name of the connection every time you want to make a new connection like this for yours mDatabase.defaultConnection="a_name_for_connection";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM