在鉻,這對我工作與html5sql。我還製作了一個使用純HTML5和基於Promise的酷查詢函數here的codepen。
function getDB(cb){
html5sql.process("SELECT * FROM sqlite_master WHERE name NOT LIKE 'sqlite\\_%' escape '\\' AND name NOT LIKE '\\_%' escape '\\'", function(txTables, rsTables, tables){
if (!tables.length) return cb(null, []);
tables.forEach(function(table){
var s = table.sql.split(',');
s[0] = s[0].replace(new RegExp('create\\s+table\\s+' + table.name + '\\s*\\(', 'i'),'');
table.fields = s.map(function(i){
return i.trim().split(/\s/).shift();
})
.filter(function(i){
return (i.indexOf(')') === -1)
})
});
cb(null, tables)
}, cb);
}
這將打擊你的(error, tables)
回調是這樣的:
[{
"type": "table",
"name": "Users",
"tbl_name": "Users",
"rootpage": 6,
"sql": "CREATE TABLE Users(\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n firstName VARCHAR(255),\n lastName VARCHAR(255),\n email VARCHAR(255),\n created TIMESTAMP DEFAULT (DATETIME('now','localtime'))\n)",
"fields": [
"id",
"firstName",
"lastName",
"email",
"created"
]
}]
注意fields
部分。即使沒有記錄,這也可以工作。正則表達式/字符串解析可能會使用一些改進,你也可以用它來獲取類型信息,但是這似乎適用於我所有的用例。另一種方法,一旦你知道的字段名,在SQL:
SELECT TYPEOF(id) as id, TYPEOF(firstName) AS firstName , TYPEOF(lastName) AS lastName, TYPEOF(email) AS email, TYPEOF(created) AS created FROM Users;
選擇的名字從那裏SYS.COLUMNS =的object_id OBJECT_ID(N'table_name') – 2013-03-05 10:37:32
你確定這個查詢網頁的SQL? – MikeLP 2013-03-05 11:08:08
不,我不確定,因爲這是爲SQL服務器.. – 2013-03-05 11:16:05