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
Developer
br14
Posts: 498
Registered: ‎06-24-2008
My Device: Not Specified

Cascades and QtSql

Can anyone help? I'm new to Qt and Cascades but thought I'd give QtSql a try.

 

I'm having a problem retrieving data from a QtSql database.

 

The CREATE and INSERT statements appear to work ok (there are no reported errors and the record ID's are returned), but when I try and retrieve data I've just inserted, no data is returned, though once again there is no sql error.

 

Is it possible I'm creating the db in a read only directory perhaps?

 

Or does anyone have example code? The quotes.db example is the only Sql example I can find and it uses the Cascades data access object approach and copies the db from the deployed app. I'd like to create the db in place.

Developer
Ebscer
Posts: 875
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha

Re: Cascades and QtSql

It is important that you have your database in a location that support both reads and writes.

 

 The code I am using is based off of the quotes example, but does not involve copying a bundled .db file and instead creates an empty table in the code. The following methods correspond to the copyDbToDataFolder() and loadDataBase() methods in the QuotesDbHelper class.

 

bool AppDbHelper::copyDbToDataFolder(const QString databaseName)
{
    QString dataFolder = QDir::homePath();
    QString newFileName = dataFolder + "/" + databaseName;
    QFile newFile(newFileName);

    return true;
}

QVariantList AppDbHelper::loadDataBase(const QString databaseName, const QString table)
{
    QVariantList sqlData;

    if (copyDbToDataFolder(databaseName))
    {
        // Load database entries using a SqlDataAccess object into a QVariantList which can be used in a GroupDataModel to present a sorted list.
        mDbNameWithPath = "data/" + databaseName;

        // Set up a SqlDataAccess object.
        SqlDataAccess sqlDataAccess(mDbNameWithPath);

        sqlDataAccess.execute("create table if not exists " + table + " (id INTEGER PRIMARY KEY ASC, item_name TEXT, group_name TEXT, details TEXT) ");

        // Set a query to obtain all entries in the table and load into our QVariantList.
        sqlData = sqlDataAccess.execute("select * from " + table).value<QVariantList>();

        if (sqlDataAccess.hasError())
        {
            DataAccessError err = sqlDataAccess.error();
            qWarning() << "SQL error: type=" << err.errorType() << ": " << err.errorMessage();

            return sqlData;
        }

        // 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 sqlData;
        }

        bool success = mDb.open();

        if (!success)
        {
            qWarning() << "Could not open database we are in trouble";
            return sqlData;
        }

        // Store the name of the table (used in the insert/update/delete functions).
        mTable = table;
    }

    return sqlData;
}

 


Read my thoughts on BlackBerry Development at news.ebscer.com
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Cascades and QtSql

you can copy the database using the target file system navigator.
i use sqllite manager in firefox to check my database.

some other tips for sql usage:
- use transactions for longer operations
- use QSqlQuery and setForwardOnly(true) to speed select statements considerably (if you only need to move forward, as most usecases do)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter