QT5和SQLite3“无查询无法取行”

时间:2021-05-10 10:59:25

i have a problem with my SQLite Database in QT5. I have table called "mytable" and im trying to save, change or load data there. The table looks like this (CODE):

我在QT5的SQLite数据库有一个问题。我有一个名为“mytable”的表,我试图在那里保存、更改或加载数据。表格如下(代码):

"mytable" ("id" INTEGER PRIMARY KEY  NOT NULL , "name" VARCHAR NOT NULL , "akcie" INTEGER NOT NULL )

Then im trying to work with this database like this (CODE):

然后我试着像这样使用这个数据库(代码):

void MainWindow::on_pushButton_save_clicked()
{
    QString jmeno, IDcko, ak;

    IDcko = ui->lineEdit_ID->text();
    jmeno = ui->lineEdit_Name->text();
    ak = ui->lineEdit_Akcie->text();

    open_connection();

    QSqlQuery query;
    query.prepare("INSERT INTO mytable(id, name, akcie) VALUES(id= \'"+ IDcko +"\', name= \'"+ jmeno +"\', akcie= \'"+ ak +"\')");

    if(query.exec())
    {
        QMessageBox::information(this, tr("SAVED"), tr("Some text."));
        close_connection();
    }
    else
    {
        QMessageBox::critical(this, tr("NOT SAVED"), query.lastError().text());
        qDebug() << query.lastError().text();
    }
}

In the same file im opening connection like this (CODE):

在同一文件中,im打开连接如下(代码):

bool MainWindow::open_connection()
{
    QSqlDatabase database;
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("‪..\\added\\test_table.sqlite");
    if(!database.open())
    {
        qDebug() << "not opened connection";
        return false;
    }
    else
    {
        qDebug() << "opened connection";
        return true;
    }
}

And im still getting error: "No query Unable to fetch row", i have been looking for the answer really hard but nothing worked. Could it be because of some includes or am I doing something wrong?

而我仍然得到错误:“没有查询无法获取row”,我一直在努力寻找答案,但什么都没有成功。是由于某些原因,还是我做错了什么?

Thank you in advance for your help!

感谢您的帮助!

1 个解决方案

#1


2  

You are not preparing the query right. Use bindings like:

您没有正确地准备查询。使用绑定:

query.prepare("INSERT INTO mytable(id, name, akcie) VALUES(:id, :name, :akcie)");
query.bindValue(":id", IDcko);
query.bindValue(":name", jmeno);
query.bindValue(":akcie", ak);    

#1


2  

You are not preparing the query right. Use bindings like:

您没有正确地准备查询。使用绑定:

query.prepare("INSERT INTO mytable(id, name, akcie) VALUES(:id, :name, :akcie)");
query.bindValue(":id", IDcko);
query.bindValue(":name", jmeno);
query.bindValue(":akcie", ak);