Solved Sqlite connection weird thing
-
@Emre-MUTLU You should also check the return value of query.prepare("SELECT col1 FROM mytable");
Try also:QSqlQuery query("SELECT col1 FROM mytable");
-
double DB::selectData() { if(db.open()) { QSqlQuery query("SELECT col1 FROM mytable"); if(query.exec()){ while(query.first()) { return query.value(0).toDouble(); } } else{ qDebug()<<query.lastError(); return -1; } }else { qDebug()<<db.lastError(); } }
-
@jsulm said in Sqlite connection weird thing:
isopen returns true. query.prepare() returns false.
-
@Emre-MUTLU said in Sqlite connection weird thing:
double DB::selectData()
{
if(db.open()) {
QSqlQuery query("SELECT col1 FROM mytable");
if(query.exec()){
while(query.first()) {
return query.value(0).toDouble();
}
} else{
qDebug()<<query.lastError();
return -1;
}
}else {
qDebug()<<db.lastError();
}
}You forgot to mention whether this version works...
-
@masa4 said in Sqlite connection weird thing:
query.prepare() returns false.
Then print the error just after query.prepare()
-
@jsulm Same error. And i think there is nothing wrong with my sql queries. Because they work if i set dbname with absolute path.
-
@jsulm the error(i replaced mytable with my table name by the way):
"no such table: mytable Unable to execute statement"
-
@masa4 said in Sqlite connection weird thing:
no such table: mytable Unable to execute statement
And does this table really exist? If a new SQLite database file is created there are no tables and you have to create them first.
-
@jsulm Yes of course this table exist. Also i can read from this table. But I have to provide full path of sqlite file to setDatabaseName method.
db.setDatabaseName("/home/projectfolder/mydbfile"); //Absolute path - Works without any problem db.setDatabaseName("mydbfile"); //Relative path - Does not work
I got this errors when i use relative path. But i want to use relative path, database file is inside the project folder.
-
@masa4 said in Sqlite connection weird thing:
db.setDatabaseName("mydbfile"); //Relative path - Does not work
Are you aware that mydbfile is not the same as /home/projectfolder/mydbfile?
The first one is inside current working directory, which is most probably not /home/projectfolder (so you're creating a new file). -
@jsulm Ow, now i see the point. The db inside the code folder, but current working folder name is actually build-project-desktop-debug. Yeah now issue clarified.
-
@masa4
Be aware that the "code" folder simply does not exist at runtime (once you deploy), it is only a thing at design time when inside Qt Creator. And at runtime you do not even know what the "current directory" will be. That is why you need to use absolute rather than relative pathnames, and you might want to look instead at QStandardPaths::StandardLocation to pick and generate a path to a suitable location for your database files.