Last active
August 29, 2015 14:05
-
-
Save manjeshpv/4922e91f125e80fb4c3f to your computer and use it in GitHub Desktop.
HTML5 SQLite getting column names, Promises
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
// jQuery Required | |
// Creating / Opening DB | |
var db = window.openDatabase("techm", "1.0", "TechM Employees", 100000) | |
function createTable(table, fields,cb) { | |
db.transaction(function (tx) { | |
tx.executeSql('create table ' + table + fields, [], function () { | |
return cb("Table Created : " + table) | |
}, function () { | |
return cb("SQL Error") | |
}); | |
}) | |
} | |
createTable("employee", "(id INTEGER,name TEXT)",function(msg){console.log(msg);}) | |
// Get columns function | |
function getColumns(table,cb) { | |
var dfd = $.Deferred(); | |
var x; | |
db.transaction(function (tx) { | |
tx.executeSql('SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "' + table + '"', [], function (tx, results) { | |
var columnParts = results.rows.item(0).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').split(','); | |
var columnNames = []; | |
for (i in columnParts) { | |
if (typeof columnParts[i] === 'string') | |
columnNames.push(columnParts[i].split(" ")[0]); | |
} | |
dfd.resolve(columnNames); | |
}, function () { | |
dfd.resolve("sQL Error"); | |
}); | |
}) | |
return dfd.done(function (value) { | |
cb(value); | |
}) ; | |
} | |
getColumns("employee",function(columns){console.log(columns)}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment