When you create QSqlQuery object the default Database must already exist.
Try with
db.setDatabaseName("sampleDB.db");
if (db.open())
{
qDebug()<<"\n**** db opened**** :";
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
}
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sampleDB.db");
if (db.open())
{
QSqlQuery query;
qDebug()<<"\n**** db opened**** :";
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
}
To copy to clipboard, switch view to plain text mode
instead of
db.setDatabaseName("sampleDB.db");
if (db.open())
{
qDebug()<<"\n**** db opened**** :";
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
}
QSqlDatabase db;
QSqlQuery query;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sampleDB.db");
if (db.open())
{
qDebug()<<"\n**** db opened**** :";
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
}
To copy to clipboard, switch view to plain text mode
Notice that QSqlQuery is created after QSqlDatabase::addDatabase
Bookmarks