ArcGIS for Developers

SqlTableModel QML Type

Editable data model for a single database table. More...

Import Statement: import ArcGIS.AppFramework.Sql 1.0

Properties

Methods

  • object get(row)

Detailed Description

The SqlTableModel component is an editable data model for a single database table. This component is returned by a call to tableModel on SqlDatabase with a tableName input. It's useful for rapidly populating components such as ListView and ComboBox and behaves similar to ListModel.

This code sample shows usage of SqlTableModel, populating a TableView object with the contents of an SqlDatabase. Be aware that, to support the TableView component, you will need to import QtQuick.Controls 1.4, rather than the latest version (you can import both).

Item {
        property FileFolder sqlFolder: AppFramework.userHomeFolder.folder("ArcGIS/Data/Sql")
        property SqlTableModel sqlTableModel

        TableView {
                id: tableView

                anchors.fill: parent

                horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOn
                flickableItem.flickableDirection: Flickable.HorizontalAndVerticalFlick

                headerDelegate: TextField {
                        text: styleData.value
                        readOnly: true
                        font.pointSize: 10
                        background: LinearGradient {
                                start: Qt.point(0, 0)
                                end: Qt.point(0, height)
                                gradient: Gradient {
                                        GradientStop { position: 0.0; color: "white" }
                                        GradientStop { position: 1.0; color: "#e0e0e0" }
                                }
                        }
                }

                rowDelegate: Rectangle {
                        color: styleData.selected ? "#0077CC" : styleData.row & 1 ? "white" : "#f5f5f5"
                        height: 20 * AppFramework.displayScaleFactor
                }

                itemDelegate: Text {
                        text: styleData.value
                        color: styleData.textColor
                        elide: styleData.elideMode
                }

                Component {
                        id: columnComponent
                        TableViewColumn { width: 100 * AppFramework.displayScaleFactor }
                }

                function setColumnsByModel(model) {
                        while (columnCount > 0)
                                removeColumn(0);
                        model.roleNames.forEach(addColumnByName);
                }

                function addColumnByName(name) {
                        addColumn(columnComponent.createObject(tableView, { role: name, title: capitalize(name) } ));
                }
        }

        SqlDatabase {
                id: db
                databaseName: sqlFolder.filePath("sample.sqlite")
        }

        function toUpperCase(c) { return c.toUpperCase(); }
        function capitalize(str) { return str.replace(/^./, toUpperCase); }

        Component.onCompleted: {
                sqlFolder.makeFolder();
                db.open();
                db.query("DROP TABLE IF EXISTS Cities");
                db.query("CREATE TABLE IF NOT EXISTS Cities ( name TEXT, country TEXT, latitude REAL, longitude REAL ); ");
                db.query("INSERT INTO Cities VALUES (:name, :country, :latitude, :longitude) ", { name: "Melbourne", country: "Australia", latitude: -37.9716929, longitude: 144.7729583 } );
                db.query("INSERT INTO Cities VALUES (:name, :country, :latitude, :longitude) ", { name: "London", country: "UK", latitude: 51.5287718, longitude: -0.2416804 } );
                db.query("INSERT INTO Cities VALUES (:name, :country, :latitude, :longitude) ", { name: "Paris", country: "France", latitude: 48.8589507, longitude: 2.2770205 } );
                db.query("INSERT INTO Cities VALUES (:name, :country, :latitude, :longitude) ", { name: "New York City", country: "USA", latitude: 40.6976637, longitude: -74.1197639 } );
                db.query("INSERT INTO Cities VALUES (:name, :country, :latitude, :longitude) ", { name: "Tokyo", country: "Japan", latitude: 35.6735408, longitude: 139.5703047 } );
                tableView.model = sqlTableModel = db.tableModel("Cities");
                tableView.setColumnsByModel(sqlTableModel);
        }
}

Property Documentation

[read-only] error : object

Returns an object containing information about the last error that occurred on the database. If there have been no errors, returns null.


[read-only] fields : SqlFieldList

Returns the record containing information about the fields of the current query.


[read-only] roleNames : QStringList

Returns a list of field names. This will be consumed as a model role used in populating components (e.g. textRole in a ComboBox).


tableName : string

Returns the name of the currently selected table.


Method Documentation

object get(row)

Returns the item at index in the data model. This allows the item data to be accessed or modified from JavaScript.

The row parameter

The row of the table to retrieve. In the code sample for this component, this parameter calls on the 'row' variable defined in the line above it.