How to create a SQLite database in Qt

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 starting the code, but I cannot find the database I created in the path you specified. Does this really create a database or just do some initialization?

If it does not create a database, then how do I create a database in the application itself? (I'm not talking about the insert.)

+7
c ++ database sqlite qt
source share
1 answer

You must also create a query that will create a non-empty database and use the correct variable name (in the code you use dbConnection and then 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)"); 
+10
source share

All Articles