Unsolved Could not insert data into Sqlite database
-
@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.
-
@SGaist
I don't understand. If you have multiple instances (e.g. so you can't use "default"), Qt wants us to later access these via the optionalconnectionName
parameter, rather than keeping the return result fromaddDatabase()
in scope, and closing & freeing on exit? OK, that's not a pattern I'm familiar with from elsewhere.In the (large) codebase I have inherited, which is my exposure to Qt, the persistent
QMainWindow
class (and/or a global singleton class) holds a created connection instance (this is done via Python 3.5mysql
module, if that matters) in a member variable, and passes it around explicitly to other dialogs etc....If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.
Hmm, I don't see any explicit deletion, or even closure. I guess Python will do that automatically on application exit (though thinking aloud I don't know when that happens vis-a-vis "QCoreApplication instance is deleted").... Should I revisit existing code architecture in this light, or maybe this is a common Python approach, I don't know.
EDIT:
Blimey! You have made me look at the existing code. So far as I can see, there are two database connections going on! One is via this Pythonmysql
module, and another is viaQSqlDatabase
(which is still stored in a persistent variable), which I guess are two quite distinct things... :( -
If you have multiple connections, keep the connection names then use QSqlDatabase::database to retrieve the QSqlDatabase object of interest when needed.