Alright, problems continuing from a previous thread, where I managed to solve the problem but now the driver is giving me headaches again (previous thread: http://www.qtcentre.org/threads/5342...ieving-records).
So I have a bunch of functions doing some querying, mainly select statements. This function works fine:
QHash<QString, int> DataWhisperer
::getShipTypeNumbers(QString reportKey
) { db.setDatabaseName(pathToApp+"/data/scanbuddy.s3db");
if (!db.open()) {
errorCode = 511;
error = true;
errorMessage = "Could not open database";
}
QHash<QString, int> shipGroups;
groupQuery.prepare("SELECT shiptypes.name FROM shiptypes "
"INNER JOIN ships ON shiptypes.id = ships.shiptype "
"INNER JOIN reports ON ships.id = reports.ship "
"INNER JOIN posts ON posts.id = reports.postid "
"WHERE posts.stamp = :thestamp");
groupQuery.bindValue(":thestamp",reportKey);
if (!groupQuery.exec() && !error) {
errorCode = 511;
error = true;
errorMessage = groupQuery.lastError().text();
} else {
while (groupQuery.next()) {
groups.append(groupQuery.value(0).toString());
}
}
db.close();
for (int i = 0; i < groups.count(); i++) {
shipGroups[groups.at(i)] = shipGroups[groups.at(i)] + 1;
}
return shipGroups;
}
QHash<QString, int> DataWhisperer::getShipTypeNumbers(QString reportKey) {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(pathToApp+"/data/scanbuddy.s3db");
if (!db.open()) {
errorCode = 511;
error = true;
errorMessage = "Could not open database";
}
QStringList groups;
QHash<QString, int> shipGroups;
QSqlQuery groupQuery(db);
groupQuery.prepare("SELECT shiptypes.name FROM shiptypes "
"INNER JOIN ships ON shiptypes.id = ships.shiptype "
"INNER JOIN reports ON ships.id = reports.ship "
"INNER JOIN posts ON posts.id = reports.postid "
"WHERE posts.stamp = :thestamp");
groupQuery.bindValue(":thestamp",reportKey);
if (!groupQuery.exec() && !error) {
errorCode = 511;
error = true;
errorMessage = groupQuery.lastError().text();
} else {
while (groupQuery.next()) {
groups.append(groupQuery.value(0).toString());
}
}
db.close();
for (int i = 0; i < groups.count(); i++) {
shipGroups[groups.at(i)] = shipGroups[groups.at(i)] + 1;
}
return shipGroups;
}
To copy to clipboard, switch view to plain text mode
While this function, almost identical, just different SQL select statement (the select statement works fine btw when ran directly on the database through SQlite administrator) fails horribly:
QHash<QString, int> DataWhisperer
::getShips(QString reportKey
) { db.setDatabaseName(pathToApp+"/data/scanbuddy.s3db");
if (!db.open()) {
errorCode = 513;
error = true;
errorMessage = "Could not open database";
} else {
std::cout <<"<h2>FOR SOME WEIRD REASON THE DATABASE IS NOW OPEN FOR BUSINESS !! </h2>";
}
QHash<int, QString> shipList = getShipList();
QHash<QString, int> ships;
QHash<int, int> report;
bool prep2 = db.isOpen(); // Here it returns false, the database is no longer open
bool prep = groupQuery.prepare("SELECT ship, amount FROM reports "
"INNER JOIN posts ON posts.id = reports.postid "
"WHERE posts.stamp = :thestamp");
groupQuery.bindValue(":thestamp",reportKey);
if (!groupQuery.exec() && !error) {
errorCode = 513;
error = true;
errorMessage = groupQuery.lastError().text();
} else {
while (groupQuery.next()) {
report.insert(groupQuery.value(0).toInt(), groupQuery.value(1).toInt());
}
}
db.close();
if(error)
std::cout <<errorMessage.toStdString() <<" " <<std::boolalpha <<prep <<" " <<prep2 <<" " <<db.lastError().text().toStdString() <<"<br />"; //See output
std::cout <<reportKey.toStdString();
QHashIterator<int, int> x(report);
QHashIterator<int, QString> i(shipList);
while (i.hasNext()) {
i.next();
while(x.hasNext()) {
x.next();
if (i.key() == x.key()) {
ships.insert(i.value(), x.value());
}
}
}
return ships;
}
QHash<QString, int> DataWhisperer::getShips(QString reportKey) {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(pathToApp+"/data/scanbuddy.s3db");
if (!db.open()) {
errorCode = 513;
error = true;
errorMessage = "Could not open database";
} else {
std::cout <<"<h2>FOR SOME WEIRD REASON THE DATABASE IS NOW OPEN FOR BUSINESS !! </h2>";
}
QHash<int, QString> shipList = getShipList();
QHash<QString, int> ships;
QHash<int, int> report;
bool prep2 = db.isOpen(); // Here it returns false, the database is no longer open
QSqlQuery groupQuery(db);
bool prep = groupQuery.prepare("SELECT ship, amount FROM reports "
"INNER JOIN posts ON posts.id = reports.postid "
"WHERE posts.stamp = :thestamp");
groupQuery.bindValue(":thestamp",reportKey);
if (!groupQuery.exec() && !error) {
errorCode = 513;
error = true;
errorMessage = groupQuery.lastError().text();
} else {
while (groupQuery.next()) {
report.insert(groupQuery.value(0).toInt(), groupQuery.value(1).toInt());
}
}
db.close();
if(error)
std::cout <<errorMessage.toStdString() <<" " <<std::boolalpha <<prep <<" " <<prep2 <<" " <<db.lastError().text().toStdString() <<"<br />"; //See output
std::cout <<reportKey.toStdString();
QHashIterator<int, int> x(report);
QHashIterator<int, QString> i(shipList);
while (i.hasNext()) {
i.next();
while(x.hasNext()) {
x.next();
if (i.key() == x.key()) {
ships.insert(i.value(), x.value());
}
}
}
return ships;
}
To copy to clipboard, switch view to plain text mode
Output from DataWhisperer::getShipTypeNumbers():
FOR SOME WEIRD REASON THE DATABASE IS NOW OPEN FOR BUSINESS !!
No query Unable to fetch row false false Driver not loaded Driver not loaded
Can someone please, for the love of god shed some light on the situation
Bookmarks