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
Regular Contributor
Posts: 54
Registered: ‎02-15-2013
My Device: Z10
Accepted Solution

using same QML datamodel with SQL database in 2 listviews

I using code from the Quotes sample app as a starting point. I'm trying to to call the same SQL database with the Quotes model to build a list in the top level of my app and then again build a list at the next heirachy.
For eg, imagine a list of different types of Quotes (funny, sad, happy etc) on the main page, and then list of people with their quotes on the 2nd level, and their actual quote in 3rd level. When I use the same quotesModel qml and asyncksource at the top level list and again at 2nd level list I get connect disconnect issues to my SQL database. How could I fix this?
Regular Contributor
Posts: 54
Registered: ‎02-15-2013
My Device: Z10

Re: using same QML datamodel with SQL database in 2 listviews

I'm able to get the 2nd and 3rd page to work because they are just transferring 1 row of data. But I want to transfer multiple rows of data to make the list on 2nd page.
Retired
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: using same QML datamodel with SQL database in 2 listviews

What sort of connection/disconnection issues? Are you using the SAME object, or creating a new one? You will probably want a new model for each level.

 

You can definitely create an app such that each level has a list in it populated using a different query on the same database.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Regular Contributor
Posts: 54
Registered: ‎02-15-2013
My Device: Z10

Re: using same QML datamodel with SQL database in 2 listviews

I am getting this error

Warning: QSqlDatabasePrivate::removeDatabase: connection 'connect' is still in use, all queries will cease to work.

Warning: QSqlDatabasePrivate::addDatabase: duplicate connection name 'connect', old connection removed.

 

I copied the quotesModel.qml to quotes2Model.qml, and used quotesModel in top page and quotes2Model at second level page. I made a second SQL database (photo.sqlite) and connected CustomSqlDataSource on my second page to it using the code below, but I still get the above error.

 

CustomSqlDataSource {

                    id: asynk2DataSource

                    source: "sql/photo.sqlite"

                    query: "SELECT * FROM phototable WHERE folder='"+ folderData.folder +"' ORDER BY ID LIMIT 10"

                    //query: "SELECT * FROM phototable ORDER BY ID LIMIT 10"

                    //source: "sql/quotes.db"

                    //query: "SELECT * FROM quotes ORDER BY lastname LIMIT 10"

                    property int loadCounter: 0

 

                    onDataLoaded: {

                        if (data.length > 0) {

                            quotes2Model.insertList(data);

                            // Load the next batch, the last parameter is set to zero to tell the data source that we are loading data to the list.

                            execute("SELECT * FROM phototable WHERE folder='"+ folderData.folder +"' ORDER BY ID LIMIT 5 OFFSET " + (10 + 5 * loadCounter), 0);

                            //execute("SELECT * FROM phototable ORDER BY ID LIMIT 5 OFFSET " + (10 + 5 * loadCounter), 0);

                            loadCounter ++;

                        }

                    }

                }

 

I think asynk2DataSource is destroying connections that asynckDataSource had made, so my queries in the second page don't work aymore. How could i fix this so that I have 1 database and can run queries from both pages in my app?

 

Regular Contributor
Posts: 54
Registered: ‎02-15-2013
My Device: Z10

Re: using same QML datamodel with SQL database in 2 listviews

Should I copy the CustomSqlDataSource class and make it CustomSqlDataSource2 and use that at the second level?

Retired
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: using same QML datamodel with SQL database in 2 listviews

You only need one datasource class, but you will want multiple instances of it.

 

The disconnection message means the new connection is now the one driving the database. Are there any other errors in the log?

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Regular Contributor
Posts: 54
Registered: ‎02-15-2013
My Device: Z10

Re: using same QML datamodel with SQL database in 2 listviews

Thanks I think I may have partially solved it by adding 

qmlRegisterType < CustomSqlDataSource > ("com.quotes.data", 1, 0, "CustomSqlData2Source");

and instantiating in second time

Regular Contributor
Posts: 54
Registered: ‎02-15-2013
My Device: Z10

Re: using same QML datamodel with SQL database in 2 listviews

but now I'm seeing an undefined error where the I'm not to access a poperty that I was passing from first database

 

I have this line at the top 

property variant folderData // 

and I'm trying to generate an image in the listview, and set the source of the image as

ImageView{

imagesource: (folderData.folder+ListItemData.name)

}

 

In labels outside the listview, folderData.folder is displaying the correct folder name from my first database

New Developer
Posts: 13
Registered: ‎04-30-2011
My Device: BlackBerry Playbook
My Carrier: Fido

Re: using same QML datamodel with SQL database in 2 listviews

I am trying to do the exact same thing and I am also using the Quotes sample as a starting point. To test this I am trying to create 2 different lists on the same page with 2 different datamodels for each. The data are on the same DB, but in different tables.  I am creating a second instance of the CustomSqlDataSource class and binding its datamodel to a different list on the same page, but when i run the app it just crashes. (while it works when i have 1 list).

 

Here is the second data that i am trying to attach to a different list.

            attachedObjects: [
                GroupDataModel {
                    id: gamesModel

                    grouping: ItemGrouping.ByFirstChar
                    sortingKeys: [ "title" ]
                },
                //Me: Custom data source for loading the games
                CustomSqlDataSource {
                    id: asynkDataSource2
                    source: "sql/sctestdb.db"
                    query: "SELECT * FROM matchups ORDER BY title LIMIT 10 "
                    property int loadCounter: 0

                    onDataLoaded: {
                        if (data.length > 0) {
                            gamesModel.insertList(data);
                        }
                    }
                }
            ] // end of attached objects
            onCreationCompleted: {
                // When the list view has been created load the data.
                asynkDataSource2.load();
            }

 

 I am not that good with C++, so i am a bit reluctant to start playing with the customsqldatasource.cpp, but I think that by calling the CustomSqlDataSource class you basically go through the whole copying data to the folder & connecting to the database, but in my case I do not need to recreate the file and connection. I basically just need to run another query for the data in a different table. Thank you in advance for your help.

 

(and Namit, if you find a solution, please let me know)

New Developer
Posts: 13
Registered: ‎04-30-2011
My Device: BlackBerry Playbook
My Carrier: Fido

Re: using same QML datamodel with SQL database in 2 listviews

I don't know why this has been marked as solved. IF you go through the thread you can see that there was no solution to the problem and that namit still has problems with his app. I have the same issue. Please advise.