Solved Sqlite connection weird thing
-
I created an sqlite db in project folder. When I try to connect it like this:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("mydbname"); db.open();
db.open() returns true. Whatever i write it in setDatabaseName function, db.open() returns true but actually its not connected, my sql queries does not run. When i put full path, it works correctly. Why this happens any idea?
-
@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). -
IIRC Sqlite does not actually create the file at open() but delays until something is written to it. If the file location is in an unwritable location it will fail at the time of the first
CREATE TABLE
etc. You are using a relative path to the database file. The file will go into the current working directory of the process at that time, which may not be writeable. This is especially the case with Windows when an executable is installed into Program Files. Often that directory is the default working directory and users cannot write there.Using an absolute path to a writable location will fix this.
-
@ChrisW67 Thanks for reply, i am using linux but i switched to absolute path.
-
Can you show your code in a little more detail?
-
@masa4 said in Sqlite connection weird thing:
Why this happens any idea?
Please add error handling: https://doc.qt.io/qt-6/qsqldatabase.html#lastError and https://doc.qt.io/qt-6/qsqlquery.html#lastError
-
@Emre-MUTLU
db.cpp:DB::DB(const QString &path) { m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(path); if(!m_db.open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; }
Its the constructor. im passing path as default value in header: db.h:
//DB(const QString& path = "/home/projectfolder/mydbname"); //this one works DB(const QString& path = "mydbname"); //but i need this one
-
@masa4 I repeat myself: Please add error handling: https://doc.qt.io/qt-6/qsqldatabase.html#lastError and https://doc.qt.io/qt-6/qsqlquery.html#lastError
-
@jsulm Thank you for advice. I added it like:
DB::DB(const QString &path) { m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(path); if(!m_db.open()) qDebug() << "Error: connection with database failed"; else qDebug() << "Database: connection ok"; qDebug() << m_db.lastError().text(); }
When i used in this way and set the path's value to a wrong value it returns empty text. no error. and output is "Database: connection ok" but actually its not ok.
-
@masa4 You wrote before: "my sql queries does not run". So, please add error handling in the code where you're executing SQL queries to see what exactly the problem is (https://doc.qt.io/qt-6/qsqlquery.html#lastError).
-
maybe your quaries not work right cause your code work for me
-
@jsulm
one function in db.cpp:double DB::selectData() { QSqlQuery query; query.prepare("SELECT col1 FROM mytable"); if(query.exec()){ query.first(); return query.value(0).toDouble(); } else{ query.lastError().text(); return -1; } }
mainwidget.h:
public: DB *db = new DB;
mainwidget.cpp:
ui->labeldb->setText(QString::number(db->selectData()));
When path value is setted to like "mydbname" the return value -1 show up on my label. If i corrected path with absolute value like "/home/projectfolder/mydbname" i got right value on my label(its 10)
But still no error messages. Error messages show as empty text, like this: "" -
@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
-
@jsulm ow sorry didnt realize it. its:
"No query Unable to fetch row"
-
@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
-
@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.