Sqlite connection weird thing
-
@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
-
@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).
@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: "" -
@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
-
@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).