Unsolved Could not insert data into Sqlite database
-
Hi I am trying to insert data into db in Qt but failed
it is my code hope your advices help to resolve the problem -
you are calling db.close() and then you execute your query. try to close the database after your queries.
-
@Fuel no I open and execute the query and than close like in my code from link) mayby you know another possible matters to resolve it
-
ok i see. i made a mistake. but why you create the necessary table after the insert queries?
-
@Fuel I paste it to show the structure of the table
-
Hi
query.exec() returns true/false
and you can use
http://doc.qt.io/qt-5/qsqlquery.html#lastError
to see why it went wrong. -
INSERT INTO " +dataB +"(depart_city,arrival_city,firstname,lastname,bot,vagon,place,privilages,distance,ticket_price)"
Maybe try to add a White Space before (depart_city
because actually your query looks like this.
INSERT INTO _people(depart_city,arrival_city,firstname,lastname,bot,vagon,place,privilages,distance,ticket_price)"
-
@mrjj I have tried the following code
if(!q.exec("INSERT INTO " +dataB +"
(depart_city,arrival_city,firstname,lastname,bot,vagon,place,privilages,distance,ticket_price)"
+"values('"+T.passangers[i].left_city+"','"+T.passangers[i].arrival_city+"','"+T.passangers[i].name+"','"
+T.passangers[i].surname+"','"+bot+"','"+T.passangers[i].wagon+"','"+T.passangers[i].place+"','"+privilages+"','"+T.passangers[i].way_distance+"','"+T.passangers[i].ticket_cost+"')"))
{
ui->textBrowser_2->append(q.lastError().text()+" "+QString::number(q.lastError().number()));
}
but the output is only -1 without any description -
try db.lastError()
-
@mark_ua_1999
So q.lastError().text() do not give any text?
even exec fails?You can also try
QString QSqlQuery::lastQuery() const
to see what all that code becomes.
Its most likely something trivial.Are there any reason you are not using bound parameters to get a cleaner looking code ?
QSqlQuery query; query.prepare("INSERT INTO person (id, forename, surname) " "VALUES (:id, :forename, :surname)"); query.bindValue(":id", 1001); query.bindValue(":forename", "Bart"); query.bindValue(":surname", "Simpson"); query.exec();
-
-
@mark_ua_1999
Check if prepare returns true -
Hi,
Before anything, check that the database opens successfully and print what happens if it fails. Also please post the configuration you used for your database.
-
Hi
as a note
http://sqlitebrowser.org/
is very useful if db is sqllite. To examine database and test queries -
-
Which version of Qt are you using ?
-
@SGaist Qt 5.3
-
Then change to positional placeholders. Support for named placeholder for the sqlite driver has been added to Qt 5.10 IIRC.
-
-
@mark_ua_1999
How many rows do you have?
And how are you reading it ?
Normally its pretty fast but if you loop all rows and say add as text
to a textbrowser it can be really slow.So please show what you are doing :)