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
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30
My Carrier: O2

Using property alias to change SQL query

Hi, I am trying to figure out how to use property alias to change the SQL query. I am just using an example database to test out the code first before I mess up my original code. Basically I am using a table structure as such:

Table: House

Columns: HouseID, HouseName

With information: (1, Gryffindor), (2, Ravenclaw), (3, HufflePuff), (4, Slytherin)

 

Table: Students

Columns: StudentID, StudentName, HouseID

With information: (1, Harry, 1), (2, Hermione, 1), (3, Ron, 1), (4, Malfoy, 4) etc...

 

In my main.qml, I display the 4 houses' names (this works okay):

 

           ListView {
                dataModel: GroupDataModel {
                    id: groupdatamodelID
                    grouping: ItemGrouping.None
                    sortingKeys: ["HouseID"]

                }
                onTriggered: {

                 //   if (indexPath.length > 1) {
                        var chosenHouse = dataModel.data(indexPath);
                        var contentpage = itemPageDefinition.createObject();
                        // it doesn't work even when I try: contentpage.listingHouse = "SELECT * FROM Students"
                        contentpage.listingHouse = "SELECT * FROM Students WHERE HouseID = '" + chosenHouse.HouseID + "'"
                        nav.push(contentpage);
                   //}
                }
                listItemComponents: [
                    ListItemComponent {
                    type: "item"
                        StandardListItem {
                            title: ListItemData.HouseName
                        }
                    }
                ]
            }

 

   attachedObjects: [
        ComponentDefinition {
            id: itemPageDefinition
            source: "ItemPage.qml"
        },
        DataSource{
            id: datasourceID
            source: "hogwarts.sqlite"
            type: DataSourceType.Sql
            query: "SELECT HouseID, HouseName FROM House"
            onDataLoaded: {
                groupdatamodelID.insertList(data)
            }
        }
    ]

 

 

ItemPage:

Page {
    property alias listingHouse: datasource1.query
    titleBar: TitleBar {
        id: titlebar
    }
    Container {
        ListView {
            id: listview
            dataModel: GroupDataModel {
            id: groupdatamodelid
            sortingKeys: ["StudentID"]
            }
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        title: ListItemData.StudentName

                    }

                }
            ]

        }
attachedObjects: [
    DataSource {
        id: datasource1
        source: "hogwarts.sqlite"
        query: listingHouse 
        onDataLoaded: {
            groupdatamodelid.insertList(data)
        }
            }
]
    }
    onCreationCompleted: {
        datasource1.load()
    }
}

 

Problem: The first list shows up alright, but the second list returns empty. Console log returns nothing helpful.

 

Thanks!

Please use plain text.
Developer
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30
My Carrier: O2

Re: Using property alias to change SQL query

[ Edited ]

I have modified my code but it is still not working.

 

In the main page, onTriggered:

                        var chosenHouse = dataModel.data(indexPath);
                        var contentpage = itemPageDefinition.createObject();
                        contentpage.houseID = chosenHouse.HouseID

 

in ItemPage.qml:

Page {
    id: pageID
    property variant houseID //tried string as well but won't work

 ...

 

attachedObjects: [
     DataSource {
         id: datasource1
         source: "hogwarts.sqlite"
         query: "SELECT * FROM Students WHERE HouseID = '" + pageID.houseID + "'"
         type: DataSourceType.Sql
         onDataLoaded: {
             groupdatamodelid.insertList(data)
             console.log("Data: " + data)
             console.log("Query: " + query)
             console.log("pageID.houseID: " + pageID.houseID)
         }
         onError: {
             console.log("Error: " + errorMessage)
         }
     }
 ]

 The query I get from Console log however, returns:

SELECT * FROM Students WHERE HouseID = ''

  even though, if I try to pass houseID through to say e.g. a Label, it does appear.

 

Please help?

Please use plain text.