First of here is my code:
#include "dbmanager.h"
#include<qdebug.h>
dbManager::dbManager()
{
db.setDatabaseName("Decoders.db");
if(!db.open())
qDebug() << "Not connected to DB.";
else if(db.open())
qDebug() << "Connected to DB.";
}
void dbManager::nametest()
{
query.prepare("SELECT name FROM Restaurant");
int idName = query.record().indexOf("name");
if(query.exec())
{
while(query.next())
{
QString name
=query.
value(idName
).
toString();
qDebug() << name;
}
}
else
{
qDebug() << query.lastError();
}
}
#include "dbmanager.h"
#include<qdebug.h>
dbManager::dbManager()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("Decoders.db");
if(!db.open())
qDebug() << "Not connected to DB.";
else if(db.open())
qDebug() << "Connected to DB.";
}
void dbManager::nametest()
{
QSqlQuery query;
query.prepare("SELECT name FROM Restaurant");
int idName = query.record().indexOf("name");
if(query.exec())
{
while(query.next())
{
QString name =query.value(idName).toString();
qDebug() << name;
}
}
else
{
qDebug() << query.lastError();
}
}
To copy to clipboard, switch view to plain text mode
The problem: I made a table called restaurant is sqlite studio, I am testing to just print the first couple names from the database to the console.
I have done this exact type of thing in a previous project with zero problems yet here it doesn't work at all. It just outputs this: "QSqlError("", "Unable to fetch row", "No query")" whenever I run the test method I have above
I have triple checked that I my prepare statement has the correct column names and everything. Any help would be great.
Edit: changed this
db.setDatabaseName("../Database/Decoders.sqlite");
Having the database file with the source code was the issue, having it in a different directory fixed the problem
Bookmarks