Unsolved Could not insert data into Sqlite database
-
@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? -
@mark_ua_1999
That i cant guess. see nothing that should be able to crash.
must be inside query.exec() then? -
Is it me or are you re-creating a new database connection each time you add a train ?
-
@SGaist yes I am recreating db connection every time
-
Why do you do that ?
-
@SGaist I habe no others ideas and what is your idea how to do it to work good
-
@mark_ua_1999
You do not want to open & close database on every event (incredibly slow)!! In some shape or form, you want to:- Open the database once on, say, application start up.
- Store the connection in a "global" or at least "persistent" variable in a class outside of any event function.
- Access the open connection throughout your application events etc.
- Close the database connection prior to application exit.
And equally you don't want to be going
CREATE TABLE
all the time, you create the table once outside of events (e.g. on database open connection if necessary) and then assume it's always present (unless you have some very unusual requirement). -
@JNBarchan Number 2 is wrong.
There's absolutely no need to store the connection in any local or global variable. QSqlDatabase already does everything you need in that aspect. It's explained in the detailed documentation of the class.