Sqlite connection weird thing
-
@masa4 said in Sqlite connection weird thing:
But still no error messages
Of course not - you are not printing the error message...
qDebug() << query.lastError().text();
-
double DB::selectData() { QSqlQuery query; query.prepare("SELECT col1 FROM mytable"); if(query.exec()){ while(query.first()) { return query.value(0).toDouble(); } } else{ qDebug()<<query.lastError(); return -1; } }
try this
@Emre-MUTLU said in Sqlite connection weird thing:
if(query.exec()){
while(query.first()) {
return query.value(0).toDouble();
}
}nope. same error. But actual problem is this code works when i use absolute path. But not for relative path.
-
did you check db is open when you trying to using query
-
double DB::selectData() { if(db.open()) { QSqlQuery query; query.prepare("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(); } }
something like this
-
double DB::selectData() { QSqlQuery query; query.prepare("SELECT col1 FROM mytable"); if(query.exec()){ while(query.first()) { return query.value(0).toDouble(); } } else{ qDebug()<<query.lastError(); return -1; } }
try this
@Emre-MUTLU You should also check the return value of query.prepare("SELECT col1 FROM mytable");
Try also:QSqlQuery query("SELECT col1 FROM mytable");
-
@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(); } }
-
@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(); } }
@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...
-
@jsulm said in Sqlite connection weird thing:
isopen returns true. query.prepare() returns false.
@masa4 said in Sqlite connection weird thing:
query.prepare() returns false.
Then print the error just after query.prepare()
-
@Emre-MUTLU You should also check the return value of query.prepare("SELECT col1 FROM mytable");
Try also:QSqlQuery query("SELECT col1 FROM mytable");
-
@masa4 said in Sqlite connection weird thing:
query.prepare() returns false.
Then print the error just after query.prepare()
-
@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.
-
@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.
-
@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). -
@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.