If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
willie44
Posts: 19
Registered: ‎01-15-2011
My Device: Not Specified

A simple SQLite example?

I am looking for a simple SQLite example. The quotes sample is too complex for me to understand at this point. C++ not my strong suit. I come from a scripting and AS3 background.

 

https://developer.blackberry.com/cascades/reference/bb__data__datasource.html

 

The code on the page makes sense to me, but my app is stuck a the "Blackberry 10" splash screen . What else is this code not telling me I need to do?

 

Thanks for any help or suggestions

Please use plain text.
Developer
zezke
Posts: 1,004
Registered: ‎12-12-2010
My Device: Z10 (red Limited Edition)
My Carrier: Mobile Vikings

Re: A simple SQLite example?

I based my code on the quotes example. It may look challenging at the start, but it's alright in the end. Here's a sample of my code.

 

bool DbHelper::instanceFlag = false;
DbHelper* DbHelper::singleton = NULL;

DbHelper* DbHelper::getInstance()
{
	if(!instanceFlag)
	{
		singleton = new DbHelper();
		instanceFlag = true;
	}
	return singleton;
}

DbHelper::~DbHelper()
{
    if (mDb.isOpen()) {
        QSqlDatabase::removeDatabase(mDbNameWithPath);
        mDb.removeDatabase("QSQLITE");
    }
    instanceFlag = false;
}

bool DbHelper::loadDataBase(const QString databaseName)
{
    if (copyDbToDataFolder(databaseName))
    {
        mDbNameWithPath = "data/" + databaseName;
        QFile copiedDbFile(mDbNameWithPath);
        if(copiedDbFile.exists())
        {
        	qDebug() << "Copied db files exists!";
        }
        else
        {
        	qDebug() << "Copied db files does not exist!";
        }
        // Set up a SqlDataAccess object.
        SqlDataAccess sqlDataAccess(mDbNameWithPath);
        if (sqlDataAccess.hasError())
        {
            DataAccessError err = sqlDataAccess.error();
            qWarning() << "SQL error: type=" << err.errorType() << ": " << err.errorMessage();
            return false;
        }
        // Open the database to enable update/insert/delete functionality (via SQL queries) using
        // a non-default connection, so we don't conflict with the database connection already setup by SqlDataAccess.
        mDb = QSqlDatabase::addDatabase("QSQLITE", "database_helper_connection");
        mDb.setDatabaseName(mDbNameWithPath);
        if (!mDb.isValid())
        {
            qWarning() << "Could not set data base name probably due to invalid driver.";
            return false;
        }
        bool success = mDb.open();
        if (!success)
        {
            qWarning() << "Could not open database we are in trouble";
            return false;
        }
        return true;
    }
    return false;
}

bool DbHelper::copyDbToDataFolder(const QString databaseName)
{
    // Since we need read and write access to the database, it has
    // to be moved to a folder where we have access to it. First
    // we check if the file already exists (previously copied).
    QString dataFolder = QDir::homePath();
    QString newFileName = dataFolder + "/" + databaseName;
    QFile newFile(newFileName);

    if (!newFile.exists())
    {
        // If the file is not already in the data folder, we copy it from the
        // assets folder (read only) to the data folder (read and write).
        // You should note that on a debug build, you will be able to write to a database
        // in the assets folder. However, for a signed application, it is not possible to
        // write to the database in the assets folder.
        QString appFolder(QDir::homePath());
        appFolder.chop(4);
        QString originalFileName = appFolder + "app/native/assets/" + databaseName;
        QFile originalFile(originalFileName);

        if (originalFile.exists()) {
            return originalFile.copy(newFileName);
        } else {
            qWarning() << "Failed to copy file data base file does not exists.";
            return false;
        }

    }
    return true;
}

 You can see I am using the singleton pattern. During app startup I initialize the databasehelper like this:

 

//Load the database
if(ComicsDbHelper::getInstance()->loadDataBase("comix.db"))
{
        qDebug() << "Database was properly loaded";
}

 And finally an example query function:

 

void DbHelper::persistData(Data data)
{
    QSqlQuery sqlQuery(mDb);
    sqlQuery.prepare("UPDATE data SET title=:title WHERE dataId=:dataId;");
    sqlQuery.bindValue(":title", data.getTitle());
    sqlQuery.bindValue(":dataid", comic.getDataId());
    if(!sqlQuery.exec())
            qDebug() << "ComicsDbHelper:: error while persisting data";
}
-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: A simple SQLite example?

Thanks! Singleton creation can be simplified a bit:

DbHelper *DbHelper::sharedInstance()
{
	static DbHelper db;
	return &db;
}

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
willie44
Posts: 19
Registered: ‎01-15-2011
My Device: Not Specified

Re: A simple SQLite example?

zezke, Thanks for the awesome information, but where do those snippets of code go? :smileyhappy: Sorry for just beginner questions.
Please use plain text.