I am trying to create a SQLite database in Qt. Here is my code:
QDir databasePath;
QString path = databasePath.currentPath()+"myDb.db";
QSqlDatabase dbConnection = QSqlDatabase:addDatabase("QSQLITE");
db.setDatabaseName(path);
db.open();
There are no errors when running the code, but I can't find the database I created in the path I defined. Does this actually create the database or does it just do some initialization?
If it does not create the database then how do I create the database within the application itself? (I am not talking about insertion.)
SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine. Used in Qt SQL Lite plugin. Configure Qt with -system-sqlite or -no-sqlite to avoid. The sources can be found in qt5/qtbase/src/3rdparty/sqlite.
To create a database, click on “Database” then “Add a database”. In the next window that appears (the left one below), click on the green “+” to create a new database.
You should also create query which will create not empty database and use correct name of variable(in your code you use dbConnection
firstly and after that - db
. For example:
QString path = "path";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
db.setDatabaseName(path);
db.open();
QSqlQuery query;
query.exec("create table person "
"(id integer primary key, "
"firstname varchar(20), "
"lastname varchar(30), "
"age integer)");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With