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
New Contributor
Posts: 5
Registered: ‎12-13-2012
My Device: alpha dev
My Carrier: TT

DataBase

Hello,

I have issues manipulating a simple database using Sqlite.

I whould like to know how to create, insert, select and manipulatre a database step by step.

thank you

 

Developer
Posts: 368
Registered: ‎06-30-2012
My Device: Playbook, Z10LE, DevAlpha C
My Carrier: Telenor, Mobitel

Re: DataBase

[ Edited ]

You cant change database if it isnt in folder which have write permissions. I personally make sqlite db with some free tool (there are dozen of free tools for creating sqlite database, tables, indexes, etc), and put it inside assets folder , preferably i make folder called sql and put db inside it. During startup of program i'm checking if db file exists into data folder and if it doesnt exist, copying my  db file there. 

Here is relevant code snippet for thsi above:

const char* const DbHandler::mCityDatabase = "data/countries.db";

DbHandler::DbHandler(bb::cascades::Application *app) :
		QObject(app) {

	QString dataFolder = QDir::homePath();
	QString newFileName = dataFolder + "/countries.db";
	QFile newFile(newFileName);

	if (!newFile.exists()) {
		//doesnt exists copy it
		QString appFolder(QDir::homePath());
		appFolder.chop(4);
		QString originalFileName = appFolder
				+ "app/native/assets/sql/countries.db";
		QFile originalFile(originalFileName);
		if (originalFile.exists()) {
			originalFile.copy(newFileName);
		} else {
			qDebug() << "Failed to copy file data base file does not exists.";

		}

	}

	db = QSqlDatabase::addDatabase("QSQLITE");
	db.setDatabaseName(mCityDatabase);
	if (!db.open()) {
		qDebug() << db.lastError();
		qFatal("Failed to connect.");

	}

	qDebug("Connected!");

}

DbHandler::~DbHandler() {

	db.close();

	qDebug("destructor!");

}

  I'm also opening db there, and in destructor make sure to close it.  Inside that DbHandler class i have different methods for selecting, inserting, updating data, etc. 

 

I expose this class to qml like this:

DbHandler *cppObject = new DbHandler(app);
	
	qml->setContextProperty("dbhandler", cppObject);

  and inside qml you can reference it and call whatever method you declare in that class (must be declared in header with Q_INVOKABLE)

 

Thats my way of doing all that db stuff, in different projects i just change methods for manipulating data, but tjhis above is always the same.

 

 

 
New Contributor
Posts: 5
Registered: ‎12-13-2012
My Device: alpha dev
My Carrier: TT

Re: DataBase

For the creation db part it's ok but i need to know how to implement all the insert, select and update. And how to use them on button events.

Thank you

Developer
Developer
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

Re: DataBase

did you look at this sample project

https://github.com/blackberry/Cascades-Samples/tree/master/quotes

 

should have all you need to integrate with sqlite with qml/c++