Unsolved Could not insert data into Sqlite database
-
@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 :)
-
@mrjj CODE - mainwindow Dialog
In dialog I create train route in main I show available train routes responding time is very long (when add new train)
; -
Hi
setCellWidget can be heavy.
But you only show 5 row here.
How many do you have?Is it the on_actionAdd_train_triggered that takes time?
-
@mrjj When I click ok in Dialog window it takes a lot of time to add new row in mainwindow have no ideas why
-
@mark_ua_1999
well you loop a file in on_actionAdd_train_triggered but do nothing with what it reads.
I do not think reading from database is the slow part and setCellWidget x5 should be that heavy unless its
a Pi board or slower pc. -
@mrjj I will be trying to show visual result adding the new train firstly and only than write to db it must help
-
@mark_ua_1999
Sounds like a good plan. IF you only show 5 at a time, its odd it should be slow. -
@mrjj I am trying to create and insert information in db in mainwindow the db is opened
but the program crashes after if(query.exec("CREATE TABLE "+e.train_name+" "
"(id integer primary key)" )) what could be a matter
void MainWindow::on_actionAdd_train_triggered()
{
Dialog e;
if(e.exec())
{
if(e.duplicate == "")
{
Add(e.Cities);QSqlQuery query; //таблиця міст через які їхатиме поїзд // train_name = s; QSqlDatabase db_ = QSqlDatabase::addDatabase("QSQLITE"); db_.setDatabaseName("C:\\SQLiteDatabaseBrowserPortable\\Data\\cities.db"); db_.open(); if(db_.isOpen()) { ui->lineEdit->setText("oPEN " + e.train_name); } if(query.exec("CREATE TABLE "+e.train_name+" " "(id integer primary key)" )) ui->lineEdit->setText("Created"); db_.close();
-
@mark_ua_1999
The easiest way to find crash is to use the debugger.
Place a break point and single step the code.
Then you can see what makes it crash. -
@mrjj it makes the crash if(query.exec("CREATE TABLE "+e.train_name+" "
"(id integer primary key)"
)) but why?