Skip to content Skip to sidebar Skip to footer

How To Make Generic Function In Database Html5?

can you please tell me how to make generic function in database HTML5.It mean I will pass name of 'Table' .Or If possible we can pass column names of table with data type ? Can you

Solution 1:

You can do this by creating an array and defining it on global level. Please find the working DEMO

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

JQUERY SCRIPTS

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


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

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

functionInsertQuery(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;
}


functionInsertIntoTable(tablename, arrColumn, arrValue) {
    var db = CreateDB();
    varQuery = InsertQuery(tablename, arrColumn, arrValue)
    db.transaction(populateDB, errorDB, successDB);

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

functionerrorDB(err) {
    alert(err.message);
}

functionsuccessDB() {
    alert('Insert Success!!!');
}
}


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

functionCreateTable(tablename, arrColumn, tx) {
    varQuery = CreateTableQuery(tablename, arrColumn);
    tx.executeSql(Query);
}

functionCreateTableQuery(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;
}

Solution 2:

The answer is difficult to answer since we don't know where to get the values from.

I suggest you to make an adjustment to the createTable function (you can pass in later what you want):

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

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

Call it like this:

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

Be aware that WebDB lost from IndexedDB in becoming the new HTML5 standard for client databases. You should use IndexedDB.

Solution 3:

We have created query where we pass tablename and array of columns, I have modified the function for you which takes other third paramater i.e datatype

functionCreateQuery(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;
}

Post a Comment for "How To Make Generic Function In Database Html5?"