Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Machinarius
Posts: 15
Registered: ‎11-11-2012
My Device: Blackberry 10 Dev Alpha B
Accepted Solution

Problem accessing assets

Hey guys

 

I have this code

void Storage::initialize() {
	isCacheValid = false;
	if(!QFile::exists("db/MoneyTracker.db")) {
		database = new SqlDataAccess("db/MoneyTracker.db");
		QFile initScript(QDir::currentPath() + "app/native/assets/dbCreatorScript.sql");
		if(!initScript.open(QIODevice::ReadOnly | QIODevice::Text))
			return;
		QTextStream initStream(&initScript);
		QString line;
		while(true) {
			line = initStream.readLine();
			if(line.isNull())
				break;
			database->execute(line);
		}
	}
	database = new SqlDataAccess("db/MoneyTracker.db");
}

 I was wondering, how can i access my dbCreatorScript.sql file? And why is the .db file never created? (Checked with the filesystem explorer) I've tried quite a couple of things and none of them have worked yet. dbCreatorScript.sql is right beside the qml files in the assets folder (It does get copied with them as well, again, checked with filesystem explorer). Am i doing something wrong? 

 

initScript.open(QIODevice::ReadOnly | QIODevice::Text)

Always return false.

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Problem accessing assets

Generally your app's working folder should be the folder above the app/ folder (which you've got covered there with the reference to dbCreatorScript.sql) and above the data/ folder... That's the only place for your app to write data, so you'll likely want to be using "data/db/MoneyTracker.db" instead.

See https://developer.blackberry.com/cascades/documentation/device_platform/filesystem/index.html for background.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Contributor
Machinarius
Posts: 15
Registered: ‎11-11-2012
My Device: Blackberry 10 Dev Alpha B

Re: Problem accessing assets

I wasn't really specific on the issue of how to open my sql file, the problem is that it won't ever read lines, it always reads null. Also, even if i go

database = new SqlDataAccess("data/db/MoneyTracker.db");

it won't create the database.
Please use plain text.
Contributor
Machinarius
Posts: 15
Registered: ‎11-11-2012
My Device: Blackberry 10 Dev Alpha B

Re: Problem accessing assets

Looked at the Cascades sample and used it to solve this issue, final code:

 

void Storage::initialize() {
	isCacheValid = false;
	QString dataPath = QDir::homePath();
	QString rootPath = QString(QDir::homePath());
	rootPath.chop(4); //chop(4) removes "data"
	QString initScriptLocation = QString(rootPath) + "app/native/assets/dbCreatorScript.sql";
	QString dbPath = QString(dataPath) + "/MoneyTracker.db";
	if(!QFile::exists(dbPath)) {
		database = new SqlDataAccess(dbPath);
		QFile initScript(initScriptLocation);
		if(!initScript.open(QIODevice::ReadOnly | QIODevice::Text))
			return;
		QTextStream initStream(&initScript);
		QString line;
		while(true) {
			line = initStream.readLine();
			if(line.isNull())
				break;
			//TODO: Implement error handling here
			database->execute(line);
		}
	}
	database = new SqlDataAccess(dbPath);
}

 

Please use plain text.