sqlite - 如果调用 setRelation,QSqlRelationalTableModel insertRecord 不起作用

标签 sqlite qt

editlistofcustomers.h

QSqlRelationalTableModel *RelationalModel;
TreeModel *CategoriesModel;
QSqlRecord Record;

编辑客户列表.cpp EditListOfCustomers::EditListOfCustomers//构造函数

RelationalModel = new QSqlRelationalTableModel(this, *SupportObj->GetDataBase());
RelationalModel->setTable(SupportObj->GetCustomersTableName());
RelationalModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
RelationalModel->select();
RelationalModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Название/имя покупателя"));
Record = RelationalModel->record();
RelationalModel->setRelation(2, QSqlRelation(SupportObj->GetCategoriesOfCustomersTableName(),
                         SupportObj->GetCategoriesOfCustomersPattern()[0].GetName(), // ID.
                         SupportObj->GetCategoriesOfCustomersPattern()[2].GetName())); // Name.
CategoriesModel = new TreeModel(QObject::tr("Категории"),
                SupportObj->GetCategoriesOfCustomersTableName(),
                SupportObj, this);


//Setup model view delegate.
ui->TV_ListOfCustomers->setModel(RelationalModel);
ui->TV_ListOfCustomers->setItemDelegate(new QSqlRelationalDelegate(ui->TV_ListOfCustomers));
ui->TV_ListOfCustomers->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->TV_ListOfCustomers->setSortingEnabled(true);

ui->TV_CategoryOfCustomer->setModel(CategoriesModel);
SupportObj->GetDataBase()->transaction()

编辑客户列表::添加客户

QString customerName = ui->LE_CustomerName->text();
if(!customerName.isEmpty())
{
    Record.setValue(SupportObj->GetCustomersPattern()[1].GetName(), QVariant(customerName)); // Name.
    int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
    Record.setValue(SupportObj->GetCustomersPattern()[2].GetName(), QVariant(categoryID)); // Category ID.
    Record.setValue(SupportObj->GetCustomersPattern()[3].GetName(), QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
    Record.setValue(SupportObj->GetCustomersPattern()[4].GetName(), QVariant(ui->LE_CustomerAddress->text())); // Address.
    Record.setValue(SupportObj->GetCustomersPattern()[5].GetName(), QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
    RelationalModel->insertRecord(-1, Record);
    if(!RelationalModel->submitAll())
    {
        QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
    }

    // Clear fields
    ui->LE_CustomerName->clear();
    ui->TV_CategoryOfCustomer->setCurrentIndex(QModelIndex());
    ui->LE_CustomerTelephoneNumbers->clear();
    ui->LE_CustomerAddress->clear();
    ui->TE_CustomerComment->clear();

    ui->LE_CustomerName->setFocus();
    ui->TV_ListOfCustomers->sortByColumn(0, Qt::AscendingOrder);
}

如果注释“RelationalModel->setRelation( ... )”,则一切正常:事务、添加、删除、提交或回滚。如果取消注释则不起作用,但显示所有内容正确(ID 替换为类别名称),但我无法使用“insertRecord”插入新行。我尝试this 建议,但没有成功。

有什么建议吗?

最佳答案

问题出在列名称中。 setRelation 将列名称 category_of_customer_id 更改为 category_of_customers_name_2

解决方案使用void setValue(int index, const QVariant &val)而不是void setValue(const QString & name, const QVariant &val) .

QSqlRecord record = RelationalModel->record();
record.setValue(1, QVariant(customerName)); // Name.
int categoryID = CategoriesModel->GetItemID(ui->TV_CategoryOfCustomer->currentIndex());
record.setValue(2, QVariant(categoryID)); // Category ID.
record.setValue(3, QVariant(ui->LE_CustomerTelephoneNumbers->text())); // Telephone numbers.
record.setValue(4, QVariant(ui->LE_CustomerAddress->text())); // Address.
record.setValue(5, QVariant(ui->TE_CustomerComment->toPlainText())); // Comment.
RelationalModel->insertRecord(-1, record);
if(!RelationalModel->submitAll())
{
    QMessageBox::warning(this, "CategoriesEditor::CategoriesEditor", SupportObj->GetDataBase()->lastError().text());
}

关于sqlite - 如果调用 setRelation,QSqlRelationalTableModel insertRecord 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151608/

相关文章:

android - 数据库升级过程中如何保留和升级SQLITE数据库

ruby-on-rails - rake 中止! ActiveRecord::StatementInvalid: SQLite3::SQLException: 没有这样的表:无法让 Rake Automation 工作

linux - Linux 上的 Qt 配置文件 (qtrc) 在哪里?

c++ - QTextEdit 需要越来越多的时间来绘制文本

c++ - 在使用 QtCreator 和 Visual Studio 构建的应用程序之间使用 Qt STL

sqlite - 加密SQLite

c# - SQLite 以整数形式保存浮点值

python - 获取 QListWidget 中选定的行

c++ - 使用 QString 替换方法用正则表达式更改文件名

.net - 如何连接到内存中的共享缓存数据库?