mardi 5 mai 2015

QT Application Unexplainable crash with Sqlite INSERT function

I'm doing calorie counter app. I'm new to QT and this error is way too hard to figure out. I've been thinking more than 3h and nothing. here is the function which causes error

 bool MainWindow::saveDB()
{
    loading = true;
    QSqlQuery q(db);
    q.exec("DELETE FROM Food");
    q.prepare("INSERT INTO Food(id,Name,Carbohydrates,Fats,Proteins,Calories) VALUES(:id,:n,:c,:f,:p,:cal)");
        for(int i = 0; i < 1; i++)
        {
                     q.bindValue(":n",ui->tableWidget->item(i,1)->text());
                     q.bindValue(":c",ui->tableWidget->item(i,2)->text().toInt());
                     q.bindValue(":f",ui->tableWidget->item(i,3)->text().toInt());
                     q.bindValue(":p",ui->tableWidget->item(i,4)->text().toInt());
                     q.bindValue(":cal",ui->tableWidget->item(i,5)->text().toInt());
                     q.bindValue(":id",ui->tableWidget->item(i,0)->text().toInt());
             if(!q.exec())
             {
                 qDebug() << q.lastError().text();
                 loading = false;
                 return false;
             }
        }
    loading = false;
    return true;
}

It is supposed to clear database and it does, and then insert values from tableWidget.

App Output

 The program has unexpectedly finished.
D:\Calc\build-CalcProto-Desktop_Qt_5_4_1_MinGW_32bit-Debug\debug\CalcProto.exe crashed

db is my QSqlDatabase object. Data is presented in tableWidget which has 6 columns, (id,Name,Carbohydrates,Fats,Proteins,Calories), qDebug doesnt show any message.I tried dozens of variations, it simply always crash. There is also loading data function which works perfectly, so the database itself is fine. Update function works fine, but the INSERT always fails. No idea whats wrong. Could you please tell me what is wrong?

Aucun commentaire:

Enregistrer un commentaire