How to create a SQLite database in Qt
37,706
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)");
Author by
user3009135
Updated on August 01, 2022Comments
-
user3009135 almost 2 years
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.)
-
McLan over 9 yearsOh I see, I didnot notice he is using different names, sorry. Hoever, I am having a problem using the same code, after hard-coded the path like this:
QString path="c://Users//me//Document//workapce//myproject//databse.db"
my app crashes. when debugging it just exit with crash .. what might be the problem -
SexyBeast about 8 yearsIs it possible to connect to a db file with a username and password?
-
Jablonski about 8 years@AttitudeMonger Try this
open()
method doc.qt.io/qt-5/qsqldatabase.html#open-1