new a model to a simple table ;
-
Re: SQL: How to insert a new record in a QSqlRelationalTableModel
you should not insert record into a relation table ,but a base table like below:
int Dialog::addNewAlbum(const QString &title, int artistId)
{QSqlTableModel* themodel = new QSqlTableModel(this); themodel->setTable(model->tableName()); themodel->select(); int id = generateAlbumId(); QSqlRecord record; QSqlField f1("albumid", QMetaType(QMetaType::Int)); QSqlField f2("title", QMetaType(QMetaType::QString)); QSqlField f3("artistid", QMetaType(QMetaType::Int)); QSqlField f4("year", QMetaType(QMetaType::Int)); f1.setValue(QVariant(id)); f2.setValue(QVariant(title)); f3.setValue(QVariant(artistId)); f4.setValue(QVariant(yearEditor->value())); record.append(f1); record.append(f2); record.append(f3); record.append(f4); themodel->insertRecord(-1,record); themodel->submitAll(); model->select();// update the relation table now;
-
Re: SQL: How to insert a new record in a QSqlRelationalTableModel
you should not insert record into a relation table ,but a base table like below:
int Dialog::addNewAlbum(const QString &title, int artistId)
{QSqlTableModel* themodel = new QSqlTableModel(this); themodel->setTable(model->tableName()); themodel->select(); int id = generateAlbumId(); QSqlRecord record; QSqlField f1("albumid", QMetaType(QMetaType::Int)); QSqlField f2("title", QMetaType(QMetaType::QString)); QSqlField f3("artistid", QMetaType(QMetaType::Int)); QSqlField f4("year", QMetaType(QMetaType::Int)); f1.setValue(QVariant(id)); f2.setValue(QVariant(title)); f3.setValue(QVariant(artistId)); f4.setValue(QVariant(yearEditor->value())); record.append(f1); record.append(f2); record.append(f3); record.append(f4); themodel->insertRecord(-1,record); themodel->submitAll(); model->select();// update the relation table now;
-
@jsulm Because I believe newcomers to this forum are allowed to post their own topic but not reply in an existing one, for whatever reason, until they get some reputation!?
Also, when you try to reply to an older topic, you get a dialog asking whether you really want to reply directly (and revive the old topic, which sometimes is not a great idea) or if you want to reply in a new topic while referring to the original one.
-
Re: SQL: How to insert a new record in a QSqlRelationalTableModel
you should not insert record into a relation table ,but a base table like below:
int Dialog::addNewAlbum(const QString &title, int artistId)
{QSqlTableModel* themodel = new QSqlTableModel(this); themodel->setTable(model->tableName()); themodel->select(); int id = generateAlbumId(); QSqlRecord record; QSqlField f1("albumid", QMetaType(QMetaType::Int)); QSqlField f2("title", QMetaType(QMetaType::QString)); QSqlField f3("artistid", QMetaType(QMetaType::Int)); QSqlField f4("year", QMetaType(QMetaType::Int)); f1.setValue(QVariant(id)); f2.setValue(QVariant(title)); f3.setValue(QVariant(artistId)); f4.setValue(QVariant(yearEditor->value())); record.append(f1); record.append(f2); record.append(f3); record.append(f4); themodel->insertRecord(-1,record); themodel->submitAll(); model->select();// update the relation table now;
@subteam themodel is leaking...
-
Re: SQL: How to insert a new record in a QSqlRelationalTableModel
you should not insert record into a relation table ,but a base table like below:
int Dialog::addNewAlbum(const QString &title, int artistId)
{QSqlTableModel* themodel = new QSqlTableModel(this); themodel->setTable(model->tableName()); themodel->select(); int id = generateAlbumId(); QSqlRecord record; QSqlField f1("albumid", QMetaType(QMetaType::Int)); QSqlField f2("title", QMetaType(QMetaType::QString)); QSqlField f3("artistid", QMetaType(QMetaType::Int)); QSqlField f4("year", QMetaType(QMetaType::Int)); f1.setValue(QVariant(id)); f2.setValue(QVariant(title)); f3.setValue(QVariant(artistId)); f4.setValue(QVariant(yearEditor->value())); record.append(f1); record.append(f2); record.append(f3); record.append(f4); themodel->insertRecord(-1,record); themodel->submitAll(); model->select();// update the relation table now;
@subteam said in new a model to a simple table ;:
QSqlTableModel* themodel = new QSqlTableModel(this);
@Christian-Ehrlicher Isn't
themodel
integrated in the parent/child hierarchy and therefore deleted with parent (i.e.Dialog
class)?! -
@subteam said in new a model to a simple table ;:
QSqlTableModel* themodel = new QSqlTableModel(this);
@Christian-Ehrlicher Isn't
themodel
integrated in the parent/child hierarchy and therefore deleted with parent (i.e.Dialog
class)?!@Pl45m4 said in new a model to a simple table ;:
integrated in the parent/child hierarchy and therefore deleted with parent (i.e. Dialog class)?!
Yes, but that's imo much too late here.