[SOLVED] Problem with QSqlDatabase
-
Hi and welcome to devnet,
First thing you should do is open your database connection only once at the start of your program since you are using SQLite. Then, if getUser is a slot, verify that you don't connect to it several times.
Hope it helps
-
I made separate function for opening the database and I call it at the begin of my program and it solved other problems except the one that matters the most so it doesn't open the database. The error messages in the output are: @QSqlError(-1, "Error opening database", "out of memory")
Unable to open input file: No such file or directory@Is there something wrong with my path or what could cause this?
I am now using this code to open the database:
@bool User::openDB() {
// Find QSLite driver db_ = QSqlDatabase::addDatabase("QSQLITE"); db_.setDatabaseName("C:/Databases/userDatabase.db.sqlite"); // Open databasee if(db_.open()) { return true; } else { qDebug() << db_.lastError(); return false; }
}@
-
How big is your database ?
-
The function is now like this:
@bool User::getUser() {
bool ret = false; if(db_.isOpen()) { QSqlQuery query("SELECT * FROM user;",db_); qDebug() << query.lastError(); if (query.first()) { name_ = query.value(1).toString(); age_ = query.value(2).toInt(); gender_ = query.value(3).toString(); height_ = query.value(4).toDouble(); weight_ = query.value(5).toDouble(); ret = true; } } return ret;
}@
-
I remember a post where a user had trouble with this QSqlQuery constructor. Try to change your function like this:
@
bool User::getUser() {bool ret = false; if(db_.isOpen()) { QSqlQuery query(db_); if(!query.exec("SELECT * FROM user")) qDebug() << query.lastError(); if (query.first()) { name_ = query.value(1).toString(); age_ = query.value(2).toInt(); gender_ = query.value(3).toString(); height_ = query.value(4).toDouble(); weight_ = query.value(5).toDouble(); ret = true; } } return ret;
}
@ -
Indeed you should have mention that earlier. Are you running this application on your Windows computer or on the Sailfish emulator/device ? In the second case I don't think it will access your hard drive to get the file hence the error "no such file or directory"
-
First in function OpenDB add this:
@ if(db_.open()) {
QStringList tables = db_.tables();
qDebug() << "Tables: " << tables;
return true;
} else {@This will show you all tables in your database if connection is successful.
- In the beginning of function getUser add this:
@QSqlDatabase db_ = QSqlDatabase::database();
if(db_.isOpen()) {
QSqlQuery query(db_);@
- In the beginning of function getUser add this:
-
Thanks for replies. I am running the application in Sailfish emulator and I think I should now get familiar with the usage of local storage and how to get database available for my Sailfish application. After that I can continue with these comments you have given.
-
I am a bit confused now. Does someone know how I could store my existing database to local storage and how I could refer to it and use it from my c++ class/object? I did found some examples on how to access database from QML but I would like to access my database from the c++ backend.
-
Hi,
This test code might help you
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);if(ConnectLocalDB()) GetData();
}
MainWindow::~MainWindow()
{
delete ui;
}bool MainWindow::ConnectLocalDB()
{
db = QSqlDatabase::addDatabase("QSQLITE");QString dbpath = "/root/testd.db"; db.setDatabaseName(dbpath); QSqlQuery query(QSqlDatabase::database()); query.exec("CREATE TABLE testtable (" "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," "testcolumn TEXT);" ); query.exec("insert into testtable(testcolumn) values('one')"); query.exec("insert into testtable(testcolumn) values('two')"); query.exec("insert into testtable(testcolumn) values('three')"); qDebug() << dbpath; return db.open();
}
bool MainWindow::GetData()
{
QSqlQuery query(QSqlDatabase::database());
QString q = "select * from testtable;";
query.exec(q);
while(query.next())
{
qDebug() << query.value(1).toString();
}
}
@ -
Thanks. I tried with that test code, but I get these errors when I try to open the test database to /root/test.db:
@QSqlDatabasePrivate::database: unable to open database: "out of memory Error opening database"
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
"/root/testd.db"@ -
Hi,
Replace the path of the Database with your path.
Which OS are you testing on ?