Solved Sqlite connection weird thing
-
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.
-
@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.