在 chrome 中,这对我有用 html5sql。我还制作了一个代码笔,它使用纯 HTML5 和一个很酷的基于 Promise 的查询功能,在这里。
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;