Last active
April 5, 2018 03:59
-
-
Save stephenquan/cdb65850f5d679c88dfe49140bbf42cf to your computer and use it in GitHub Desktop.
appstudio-sqldatabase-drivername-sample.qml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This sample demonstrates connecting to a variety of different databases using SqlDatabase. | |
// It makes use of Sql.availableDrivers and SqlDatabase.driverName to present you a list of database types. | |
// QSQLITE is supported on all platforms (Android, iOS, Windows, macOS, linux) | |
// QODBC works best on Windows, but, somewhat difficult to work on macOS and Linux. | |
// QPSQL works best on macOS, but, somewhat difficult to work on Windows and Linux. | |
// QMYSQL should work okay on Windows, macOS and Linux. | |
import QtQuick 2.7 | |
import QtQuick.Controls 2.1 | |
import QtQuick.Layouts 1.3 | |
import ArcGIS.AppFramework 1.0 | |
import ArcGIS.AppFramework.Sql 1.0 | |
Item { | |
property var driverNames: [ "QSQLITE", "QODBC", "QMYSQL", "QPSQL" ].filter(isAvailableDriver) | |
property string driverName: "QSQLITE" | |
property SqlDatabase dbQSQLITE: SqlDatabase { driverName: "QSQLITE" } | |
property SqlDatabase dbQODBC: SqlDatabase { driverName: "QODBC" } | |
property SqlDatabase dbQMYSQL: SqlDatabase { driverName: "QMYSQL" } | |
property SqlDatabase dbQPSQL: SqlDatabase { driverName: "QPSQL" } | |
property SqlDatabase db: this["db" + driverName] | |
property string errorString: "" | |
ColumnLayout { | |
anchors.fill: parent | |
anchors.margins: 10 | |
TextField { | |
id: hostNameTextField | |
Layout.fillWidth: true | |
placeholderText: qsTr("HostName") | |
selectByMouse: true | |
onTextChanged: errorString = "" | |
} | |
TextField { | |
id: databaseNameTextField | |
Layout.fillWidth: true | |
placeholderText: qsTr("Database Name") | |
selectByMouse: true | |
onTextChanged: errorString = "" | |
} | |
TextField { | |
id: userNameTextField | |
Layout.fillWidth: true | |
placeholderText: qsTr("User Name") | |
selectByMouse: true | |
onTextChanged: errorString = "" | |
} | |
TextField { | |
id: passwordTextField | |
Layout.fillWidth: true | |
placeholderText: qsTr("Password") | |
selectByMouse: true | |
echoMode: TextInput.PasswordEchoOnEdit | |
onTextChanged: errorString = "" | |
} | |
RowLayout { | |
Layout.fillWidth: true | |
ComboBox { | |
id: comboBox | |
model: driverNames | |
onCurrentTextChanged: setDriverName(currentText) | |
} | |
Button { | |
text: qsTr("Open") | |
enabled: !db.isOpen | |
onClicked: dbOpen() | |
} | |
Button { | |
text: qsTr("Close") | |
enabled: db.isOpen | |
onClicked: db.close() | |
} | |
} | |
TextArea { | |
Layout.fillWidth: true | |
Layout.fillHeight: true | |
text: errorString | |
readOnly: true | |
color: "red" | |
wrapMode: TextInput.WrapAtWordBoundaryOrAnywhere | |
} | |
} | |
function isAvailableDriver(driverName) { | |
return Sql.availableDrivers.indexOf(driverName) != -1; | |
} | |
function setDriverName(value) { | |
driverName = value; | |
hostNameTextField.text = db.hostName; | |
databaseNameTextField.text = db.databaseName; | |
userNameTextField.text = db.userName; | |
passwordTextField.text = db.password; | |
errorString = ""; | |
} | |
function dbOpen() { | |
errorString = ""; | |
db.close(); | |
db.hostName = hostNameTextField.text; | |
db.databaseName = databaseNameTextField.text; | |
db.userName = userNameTextField.text; | |
db.password = passwordTextField.text; | |
if (!db.open()) { | |
errorString = JSON.stringify(db.error, undefined, 2); | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment