Solved Could not insert new data in sqlite db
-
Hi I am trying to insert data in db the connection is set but i get masseges( QSqlQuery::prepare: no driver, no query could not fetch the row ) how could i resolve it thanks
QSqlQuery r;
QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE");
db1.setDatabaseName("C:\SQLiteDatabaseBrowserPortable\Data\cities.db");
db1.open();
if(db1.isOpen())
{
QProgressBar l = ui->tableWidget->cellWidget(ui->tableWidget->rowCount()-1,5)->findChild<QProgressBar>();
l->setMinimum(0);
l->setMaximum(train_.passangers.size());
l->setValue(0);
this->setCursor(Qt::WaitCursor);
QApplication::processEvents();for(int i = 0; i< train_.passangers.size();i++) { l->setValue(i+1); // r.clear(); if(r.prepare("INSERT INTO "+name+" (depart_city,arrival_city,firstname,lastname,bot,vagon,place,privileges,distance,ticket_price,depart_time,arrival_time)" "VALUES (:depart_city,:arrival_city,:first_name,:lastname,:bot,:vagon,:place,:privileges,:distance,:ticket_price,:depart_time,:arrival_time)")) ui->textBrowser->append("Yes"); else ui->textBrowser->append("--"+r.lastError().text()); r.bindValue(":depart_city",train_.passangers[i].left_city); r.bindValue(":arrival_city",train_.passangers[i].arrival_city); r.bindValue(":first_name",train_.passangers[i].name); r.bindValue(":lastname",train_.passangers[i].surname); r.bindValue(":bot","yes"); r.bindValue(":vagon",train_.passangers[i].wagon); r.bindValue(":place",train_.passangers[i].place); r.bindValue(":privileges","no"); r.bindValue(":distance",train_.passangers[i].way_distance); r.bindValue(":ticket_price",train_.passangers[i].ticket_cost); r.bindValue(":depart_time",train_.passangers[i].left_time_); r.bindValue(":arrival_time",train_.passangers[i].arrival_time_); if(r.exec()) ui->textBrowser->append("Yes"); else ui->textBrowser->append(r.lastError().text()); QCoreApplication::processEvents(); } this->setCursor(Qt::ArrowCursor); }
-
Hi,
You creare the
r
object before there's any database connection available.Like it was already suggested several times: setup your database once at application startup, that will simplify your life.
-
@SGaist thanks