1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import QtQuick 2.4
import QtQuick.LocalStorage 2.0
Item {
/*Propriété de l'item à rensigner */
property var db : getDatabase("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000)
property string dbTable : "Greeting"
/* Exemple de codes */
function printResult(result){
for(var key in result){
var dic = result[key]
console.log(key, dic.salutee, dic.salutation)
}
}
Component.onCompleted: {
createTable('salutation TEXT, salutee TEXT')
insertValueByCol({"salutee":"Bob", "salutation":"hello"})
insertValueByCol({"salutee":"Bob", "salutation":"hello"})
insertValue(["hello","Toto"])
insertValue(["salut","Jiyuu"])
var newData = getAll()
var newData2 = filter("salutation = 'hello'")
printResult(newData2)
}
/* Fonctions internes */
function getDatabase(name, version, descriptif, taille) {
return LocalStorage.openDatabaseSync(name, version , descriptif, taille);
}
function createTable(dbQuery) {
db.transaction(
function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS ' +dbTable+ ' ('+dbQuery+')');
}
)
}
function insertValueByCol(dbDict) {
var dbColumn = []
var dbValue = []
var nbVal = ''
for(var key in dbDict) {
dbColumn.push(key)
dbValue.push(dbDict[key])
nbVal += '?,'
}
nbVal = nbVal.substring(0, nbVal.length - 1);
db.transaction(
function(tx) {
tx.executeSql('INSERT INTO ' +dbTable+'('+dbColumn+')'+ ' VALUES('+nbVal+')', dbValue);
}
)
}
function insertValue(dbValue) {
var nbVal = ''
for(var i in dbValue) {
nbVal += '?,'
}
nbVal = nbVal.substring(0, nbVal.length - 1);
db.transaction(
function(tx) {
tx.executeSql('INSERT INTO ' +dbTable+ ' VALUES('+nbVal+')', dbValue);
}
)
}
function getAll() {
var result
db.transaction(
function(tx) {
var rs = tx.executeSql('SELECT * FROM '+dbTable);
var info = tx.executeSql('PRAGMA table_info('+dbTable+') ')
var col = []
var r = []
for(var column = 0; column < info.rows.length; column++) {
col.push(info.rows.item(column).name)
}
for(var i = 0; i < rs.rows.length; i++){
var dic = {}
for(var key in col) {
var keyDic = col[key]
var valueDic = rs.rows.item(i)[keyDic]
dic[keyDic] = valueDic
}
r.push(dic)
}
result = r
}
)
return result
}
function filter(query) {
var result
db.transaction(
function(tx) {
var rs = tx.executeSql('SELECT * FROM '+dbTable+' WHERE '+query);
var info = tx.executeSql('PRAGMA table_info('+dbTable+') ')
var col = []
var r = []
for(var column = 0; column < info.rows.length; column++) {
col.push(info.rows.item(column).name)
}
for(var i = 0; i < rs.rows.length; i++){
var dic = {}
for(var key in col) {
var keyDic = col[key]
var valueDic = rs.rows.item(i)[keyDic]
dic[keyDic] = valueDic
}
r.push(dic)
}
result = r
}
)
return result
}
} |