QSqlQuery query;...
myBD::myBD(const QString& bd, QWidget* wid1) : QWidget(wid1){ model = new QSqlTableModel; model->setTable(bd); model->select(); model->setEditStrategy(QSqlTableModel::OnFieldChange); model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); model->setHeaderData(1, Qt::Horizontal, QObject::tr("Pole1")); model->setHeaderData(2, Qt::Horizontal, QObject::tr("Pole2")); QTableView *view = new QTableView; view->setModel(model); view->setColumnHidden(0, true); setWindowTitle(tr("Baza dannih")); QPushButton *quitButton = new QPushButton("&Exit"); QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(quitButton); usrName = new QLineEdit; usrValue = new QDoubleSpinBox; buttonLayout->addWidget(usrName); buttonLayout->addWidget(usrValue); QPushButton *upd2Button = new QPushButton("&Update"); QObject::connect(upd2Button, SIGNAL(clicked()), this, SLOT(updateString())); buttonLayout->addWidget(upd2Button); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(view);mainLayout->addLayout(buttonLayout); setLayout(mainLayout); resize(600,500);}bool myBD::updateString(){ QSqlQuery query; query.prepare("update bdName set Pole1 = ? where Pole2 = ?"); query.addBindValue(usrValue->text()); query.addBindValue(usrName->text()); model->submitAll(); return query.exec();}
model->setQuery(QString("update bdName set Pole1 = %1 where Pole2 = %2").arg(usrValue->text()).arg(usrName->text()));
ui->tableView->model()->submit();
QSqlQuery query; query.prepare("update bdName set Pole1 = ? where Pole2 = ?"); query.addBindValue(usrValue->text()); query.addBindValue(usrName->text()); query.exec(); model->clear(); model->setQuery("SELECT * FROM bdName");
C++ (Qt)bool myBD::updateString(){ QSqlQuery query; query.prepare("update bdName set Pole1 = ? where Pole2 = ?"); query.addBindValue(usrValue->text()); query.addBindValue(usrName->text()); bool ok = query.exec(); if(ok) model->select(); return ok;}