4

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):

"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):

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?

Thank you in advance for your help!

2
  • What does QSqlQuery::lastError() say? Commented Jul 11, 2015 at 15:25
  • @cmannett85 It still says: "No query Unable to fetch row" Commented Jul 12, 2015 at 11:04

2 Answers 2

3

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);    
Sign up to request clarification or add additional context in comments.

8 Comments

print out the query you are preparing with query.executedQuery()
@PetrBečka you should use bindings as much as possible anyway to prevent SQL injection attacks.
@Miki query.executedQuery() shows me this: "INSERT INTO mytable(id, name, akcie) VALUES(id= 'idcko', name= 'jm', akcie= 'akc')" and I am using bindValues as you told me..
remove the (') from the prepare.... that should work.. sorry for the typo. Aslo remove (id=). See update code in the answer
@Miki Ok, something has changed, my query.prepare() looks exactly like yours and im getting query.lastError() like: " Parameter count mismatch" and my query.executedQuery() is saying: "INSERT INTO mytable(id, name, akcie) VALUES(?, ?, ?)"
|
0

You did not execute the query. You should use bindings like:

query.prepare("INSERT INTO mytable(id, name, akcie) VALUES(?, ?, ?)");
query.addBindValue(IDcko);
query.addBindValue(jmeno);
query.addBindValue(ak); 
query.exec();

or, like @Miki mentioned, with the help of query.bindValue(":id", IDCko) function. The last way will be to write the query text straightly (without the need to separately call query.exec():

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.