SQLITE no query unable to fetch the row
-
Hi everybody! I have a code'structure like this :
include
login.h
paginaprincipale.h
sc_assistivo.h
source
login.cpp
paginaprincipale.cpp
sc_assistivo.cpp
ui
login.ui
paginaprincipale.ui
sc_assistivo.uiI save data on a db in sqlite ahd it work fine for the file login and paginaprincipale, but when i do the same for the sc_assistivo file i get the error no quey unable to fetch the row. And i did the same identical things for all the three files.
Does anybody have an idea?
i works with cmake, but the file cmakelist seems to be correct. Moreover when i check if the db is connected i've got positive answer. the trouble starts when i try to modify the db after clicking a push putton (which is declared as a private slot)
ps. i don't add the database path every time, i just have done it once and then in other files i use
QSqlDatabase mydb2 = QSqlDatabase::database();QSqlDatabase mydb3 = QSqlDatabase::database();
-
Please show some code - we can't do anything with the filenames of your project...
-
Hi,
If you only have one database and use the default connection, you don't need to do that.
-
As stated in the QSqlDatabase documentation, don't keep class member variable of that type.
Beside that, in the constructor, you are shadowing that variable so the member variable isn't valid and if you use it later on, you'll be trying to access an invalid connection.
Therefore: start by removing these QSqlDatabase member variables.
-
As stated in the QSqlDatabase documentation, don't keep class member variable of that type.
Beside that, in the constructor, you are shadowing that variable so the member variable isn't valid and if you use it later on, you'll be trying to access an invalid connection.
Therefore: start by removing these QSqlDatabase member variables.
-
@SGaist sorry, i can't understand, which variable are you referring to? i try to remove QSqlDatabase member but i get that the db is not connected.
-
i remove that but the i get the same error, i've created a connection with the database in another file, and then i use the same connection in this, so i don't need to use any path. the strange thing is that i also create a query to select name and last name in the database and shot in my window, and that works! but then, when i move into the check button function i can't do anything with db. so why in the ui(new Ui::sc:assistivo) i can read data and in the void function no? thank you all for taking the time
-
i remove that but the i get the same error, i've created a connection with the database in another file, and then i use the same connection in this, so i don't need to use any path. the strange thing is that i also create a query to select name and last name in the database and shot in my window, and that works! but then, when i move into the check button function i can't do anything with db. so why in the ui(new Ui::sc:assistivo) i can read data and in the void function no? thank you all for taking the time
Ok, its hard to overview with not all code but can we just note the following rules.
1: dont store the database variable. Qt always keeps track of this for you.
So its perfectly fine to open it somewhere and then uses query some where else.Only exception is if you want to use multiple databases at same file. As in different files.
Then we need to use names pr database and give to query.2: If Sqllite dont find the file you ask it to open, It creates a new blank one and opens it
so it looks good but it has no data.3: you must tell it a file or memory
db.setDatabaseName(":memory:");
or
db.setDatabaseName("c:/folder/test.db");small example just make sure its clear.
bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); if (!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel); return false; } QSqlQuery query; qDebug() << "table:" << query.exec("create table person (id int primary key, " "firstname varchar(20), lastname varchar(20), num int )"); query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')"); query.exec("insert into person values(102, 'Christine', 'Holand','2')"); query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')"); query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')"); query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')"); return true; } // here we dont use the QSqlDatabase db variable at all. we just use query void queryToCsv() { QSqlQuery query; query.prepare(" select * from person;"); QFile csvFile ("output.csv"); if (!csvFile.open(QFile::WriteOnly | QFile::Text)) { qDebug("failed to open csv file"); return; } if (!query.exec()) { qDebug("failed to run query"); return; } QTextStream outStream(&csvFile); outStream.setCodec("UTF-8"); while (query.next()) { const QSqlRecord record = query.record(); for (int i = 0, recCount = record.count() ; i < recCount ; ++i) { if (i > 0) { outStream << ','; } outStream << escapedCSV(record.value(i).toString()); } outStream << '\n'; } }