【发布时间】:2022-01-19 22:56:27
【问题描述】:
我用数据库中的项目填充组合框。当我尝试添加新项目、删除所有项目并再次添加它们时,如果正在更改 db,我会看到以下错误:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlQuery::value: not positioned on a valid record created
terminate called after throwing an instance of 'std::invalid_argument' what(): stoi
21:41:04: Debugging of C:\Users\79107\Downloads\build-food_calculator-Desktop_Qt_6_2_2_MinGW_64_bit-Debug\debug\food_calculator.exe has finished with exit code 3.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
MainWindow::foodListConstructor();//function, that fills the comboBox
}
void MainWindow::foodListConstructor()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("food_list.db");
db.open();
QSqlQuery query("SELECT food_name FROM food", db);
if(query.isActive())
{
while(query.next())
{
ui->comboBox->addItem(query.value(0).toString());
}
}
}
void MainWindow::on_action_3_triggered()
{
AddFood af(this);// in this new window a user writes what he wants to add
af.setModal(true);
af.exec();
this->ui->comboBox->clear();
this->ui->comboBox->addItem("test");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("food_list.db");
db.open();
QSqlQuery query1("SELECT food_name FROM food", db);
if(query1.isActive())
{
while(query1.next())
{
ui->comboBox->addItem(query1.value(0).toString());
}
}
如何使它工作而不是重复项目(如果我删除“this->ui->comboBox->clear();”会发生这种情况)?
【问题讨论】:
标签: c++ sql sqlite qt debugging