SQLite DB at Runtime
-
Hi,
That is correct and you had it wrong in your original code.
@silenzio76 said in SQLite DB at Runtime:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
Change ":memory:" to the path you want to use and it should work correctly now.
One additional thing, you should use QStandardPaths to get a suitable folder to store your database. On most OS, installing an application is done in a read-only part of your system, so putting your database in the same folder as your application will fail.
[edit: fixed unclear phrase about path SGaist]
@sgaist Obvously it works then how can i create the db file at runtime?
-
Meaning allow the user to select the folder where to store the database ?
-
@sgaist that will be in a second phase, for now I need create the db file( db.sqlite ?)at runtime
-
If the file doesn't exist, it will be created. That's how SQLite works (it's nothing related to the plugin).
-
If the file doesn't exist, it will be created. That's how SQLite works (it's nothing related to the plugin).
@sgaist ok, if will be created my question is where is put the file qith the ":memory: condition after the app is terminated?
Because the next time I will need to connect to it and retrieve the saved datas. -
There's no file created when using the special keyword ":memory:".
-
@sgaist the problem is that, I need to create the file at the first start of the app.
-
@silenzio76 said in SQLite DB at Runtime:
the problem is that, I need to create the file at the first start of the app.
So where is your problem - simply pass the filename to setDatabaseName() instead ":memory:"...
-
@silenzio76 said in SQLite DB at Runtime:
the problem is that, I need to create the file at the first start of the app.
So where is your problem - simply pass the filename to setDatabaseName() instead ":memory:"...
@christian-ehrlicher done but don't work, it's in the first post:
db.setDatabaseName(dbPath);
-
In your first post, your db object is invalid.
addDatabase
is a static method that returns a QSqlDatabase object that is the one that you should use to setup the database connection. Hence my comment on your new version using:memory:
., you are using QSqlDatabase correctly on that one. So as already said, you can now use a path to a database file. If it's still not working, then check the errors and also check that you have the write permission on the folder you want to store your database in. -
@Silenzio76
I don't have the time to read your code, but take a look at my SQLiteSave library: https://github.com/Oshio09/Oshio_Qt_Static_LibsIts not documented but you will find the interface very intuitive. Try to maintain a copy of the repository, because I just created after I saw your question. I may delete or rename it in the future.
There is a QT Test project that I wrote, read and use as an example on how to use the library.
I don't know how much you understand about Qt so I wrote some simple steps on how to run the tests at the README file.I wrote this lib last week so I didn't had time to do an extensive test, try to run and let me know of any errors. As @SGaist pointed out QStandardPaths is good practice, SQLiteSave makes use of it.
-
@Silenzio76
I don't have the time to read your code, but take a look at my SQLiteSave library: https://github.com/Oshio09/Oshio_Qt_Static_LibsIts not documented but you will find the interface very intuitive. Try to maintain a copy of the repository, because I just created after I saw your question. I may delete or rename it in the future.
There is a QT Test project that I wrote, read and use as an example on how to use the library.
I don't know how much you understand about Qt so I wrote some simple steps on how to run the tests at the README file.I wrote this lib last week so I didn't had time to do an extensive test, try to run and let me know of any errors. As @SGaist pointed out QStandardPaths is good practice, SQLiteSave makes use of it.
@oshio Thank You, you'ra are very usefull!!!