可以获取包含表的所有列名的行,如下所示:
|id|foo|bar|age|street|address|
我不喜欢用Pragma table_info(bla)
.
SELECT sql FROM sqlite_master WHERE tbl_name = 'table_name' AND type = 'table'
然后使用Reg Exp(很容易)解析这个值,它看起来类似于: [(.*?)]
或者你可以使用:
PRAGMA table_info(table_name)
如果您.headers on
在执行查询之前使用命令行shell到SQLite .您只需要在给定的会话中执行此操作一次.
这有助于HTML5 SQLite:
tx.executeSql('SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "your_table_name";', [], function (tx, results) { var columnParts = results.rows.item(0).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').split(','); ///// RegEx var columnNames = []; for(i in columnParts) { if(typeof columnParts[i] === 'string') columnNames.push(columnParts[i].split(" ")[0]); } console.log(columnNames); ///// Your code which uses the columnNames; });
您可以使用您的语言重用正则表达式来获取列名称.
更短的选择:
tx.executeSql('SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "your_table_name";', [], function (tx, results) { var columnNames = results.rows.item(0).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').replace(/ [^,]+/g, '').split(','); console.log(columnNames); ///// Your code which uses the columnNames; });