Ressource file not found in Linux but works in Windows
-
Hi, I am using a QRessource to fetch an SQL to run. However it is getting the file in windows only.
@QFile createTableSQLFile(":/sql/dbinstaller.sql");@
Here's my meradatabase.h
@class MeraDatabase
{
public:
static QSqlQuery exec(QString sql);
static QSqlDatabase getDB();protected:
static QSqlDatabase init();
private:
static const QSqlDatabase db;
static bool install(QSqlDatabase temp);
};@meradatabase.cpp
@#include "meradatabase.h"
#include <QtSql>
#include "iostream"
#include "string"
#include <fstream>
#include <QtDebug>
#include <QSqlDatabase>using namespace std;
const QSqlDatabase MeraDatabase::db = MeraDatabase::init();
bool MeraDatabase::install(QSqlDatabase temp)
{
temp.open();
qDebug() << "Installing..";//installlock.open(QIODevice::WriteOnly | QIODevice::Text); //QTextStream out(&installlock); //out << "locked"; QString sql; QFile createTableSQLFile(":/sql/dbinstaller.sql"); if(createTableSQLFile.open(QIODevice::ReadOnly)){ // This is failing in Linux <<<<<<<<<<<<<<<<<<< //temp.exec("CREATE DATABASE fuzzycompostdb"); sql = createTableSQLFile.readAll(); temp.exec(sql); qDebug() << ".. Creating tables.."; qDebug() << temp.lastError(); QFile addDefaultSubstrates(":/sql/defaultsubstrates.sql"); if(addDefaultSubstrates.open(QIODevice::ReadOnly)){ sql = addDefaultSubstrates.readAll(); temp.exec(sql); qDebug() << ".. Adding Default Values.." <<sql; qDebug() << temp.lastError(); } } return true;
}
QSqlDatabase MeraDatabase::init()
{
qDebug() << "Getting DB connection";
QString dbName = "fuzzyDB";
QString dbType = "QSQLITE";
//QSqlDatabase db;
QSqlDatabase temp;
if(dbType == "QSQLITE"){
temp = QSqlDatabase::addDatabase("QSQLITE");
qDebug() << "sqlite temp..";
//temp.addDatabase("QSQLITE");
temp.setDatabaseName(dbName + "db3");
QFile installlock("installlock");
if(!installlock.exists()){
install(temp);
}
}QSqlQuery result = temp.exec("select * from substrate_type"); qDebug() << temp.lastError(); qDebug() << "name"; while(result.next()){ qDebug() << "name" <<result.value("name"); } return temp;
}
QSqlQuery MeraDatabase::exec(QString sql)
{
return db.exec(sql);
}@If i use the resource outside this class, it works fine on both Linux and windows. Dunno why my Linux Qt can't find the file.
Best Regards
Nayar -
Make sure all the paths are correct. Windows uses different build directories by default, it can cause a lot of hard-to-debug bugs.