如何在数据库HTML5中创建泛型函数?

时间:2014-02-12 10:01:24

标签: jquery html5 cordova

请告诉我如何在数据库HTML5中创建泛型函数。这意味着我将传递“表”的名称。或者如果可能,我们可以传递具有数据类型的表的列名?

请举例说明一下吗?

我在使用时遇到错误 http://jsfiddle.net/RnheY/1/

var html5rocks = {};
html5rocks.webdb = {};
html5rocks.webdb.db = null;
html5rocks.webdb.open = function() {
 var dbSize = 5 * 1024 * 1024; // 5MB
  html5rocks.webdb.db = openDatabase("DataBaseName", "1.0", "Todo manager", dbSize);
}


html5rocks.webdb.createTable = function(tableName) {
  var db = html5rocks.webdb.db;

  db.transaction(function(tx) {
    tx.executeSql("CREATE TABLE IF NOT EXISTS "+tableName+"(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)", []);
  });
}
$(document).ready(function(){
$( "#test" ).click(function() {
  alert( "Handler for .click() called." );
      html5rocks.webdb.open();
  html5rocks.webdb.createTable("Student");
});
                            });

3 个答案:

答案 0 :(得分:1)

您可以通过创建数组并在全局级别定义它来完成此操作。 请找到有效的DEMO

<input type="text" id="UserName" name="UserName" placeholder="Username" value="Rohit">
<input type="password" id="Password" name="Password" placeholder="Password" value="Password">
<input type="submit" id="submit" value="Log in" data-mini="true">

JQUERY SCRIPTS

var arrcolumnName = ['UserName', 'Password'];
var tablename = 'tbl_Login'


$('#submit').click(function () {
    submitForm();
});

function submitForm() {
    var _UserName = $('#UserName').val();
    var _Password = $('#Password').val();
    var arrValue = _UserName + '|' + _Password;
    var arr = arrValue.split("|");
    InsertIntoTable(tablename, arrcolumnName, arr);
 }

function InsertQuery(tablename, arrColumn, arrValues) {
    var query = "";
    query += "INSERT INTO " + tablename + " (";
    for (i = 0; i < arrColumn.length; i++)
    query += arrColumn[i] + ",";

    query = query.substring(0, query.length - 1);
    query += ") VALUES ("
    for (i = 0; i < arrValues.length; i++)
    query += "'" + arrValues[i] + "',";
    query = query.substring(0, query.length - 1);
    query += ")"
    return query;
}


function InsertIntoTable(tablename, arrColumn, arrValue) {
    var db = CreateDB();
    var Query = InsertQuery(tablename, arrColumn, arrValue)
    db.transaction(populateDB, errorDB, successDB);

function populateDB(tx) {
    CreateTable(tablename, arrcolumnName, tx);
    tx.executeSql(Query);
}

function errorDB(err) {
    alert(err.message);
}

function successDB() {
    alert('Insert Success!!!');
}
}


function CreateDB() {
    var db = window.openDatabase('TrialDB1', '', 'my first database', 5 * 1024 * 1024);
    return db;
}

function CreateTable(tablename, arrColumn, tx) {
    var Query = CreateTableQuery(tablename, arrColumn);
    tx.executeSql(Query);
}

function CreateTableQuery(tablename, arrColumn) {
    var query = "";
    query += "CREATE TABLE IF NOT EXISTS " + tablename + " (";
    for (i = 0; i < arrColumn.length; i++)
    query += arrColumn[i] + ",";

    query = query.substring(0, query.length - 1);
    query += ")"
    return query;
}

答案 1 :(得分:0)

答案很难回答,因为我们不知道从哪里获取值。

我建议你调整一下createTable函数(你可以稍后传入你想要的内容):

html5rocks.webdb.createTable = function(tableName, fields) {
  var db = html5rocks.webdb.db;

  db.transaction(function(tx) {
    tx.executeSql("CREATE TABLE IF NOT EXISTS "+tableName+"("+fields+")", []);
  });
}

这样称呼:

html5rocks.webdb.createTable("Student", "ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME");

请注意,WebDB从IndexedDB中丢失,成为客户端数据库的新HTML5标准。您应该使用IndexedDB。

答案 2 :(得分:0)

我们创建了查询表,我们传递了表名和列数组,我已经为你修改了函数,它接受了其他第三个参数,即数据类型

function CreateQuery(tablename, arrColumn,arrDataType) {
    var query = "";
    query += "CREATE TABLE IF NOT EXISTS " + tablename + " (";
    for (i = 0; i < arrColumn.length; i++)
        query += arrColumn[i] + " " + arrDataType[i] + ",";

    query = query.substring(0, query.length - 1);
    query += ")"
    return query;
}