Saturday, December 1, 2018

How to create SQLite Database & access with in Kony



Before refer below code check: SQLite_App its easy to understand
/*****************************************************************
Name    : createDB
Purpose : To create the database with employee_details table
 ******************************************************************/
function createDB() {
    webSQLFlag = 1;
    baseObjectId = kony.db.openDatabase("webSqlDB",
        "1.0",
        "Sample SQL Database",
        5 * 1024 * 1024); // 5MB database

    kony.db.transaction(baseObjectId,
        createTable,
        commonErrorCallback,
        commonVoidcallback);

}


/*****************************************************************
Name    : createTable
Purpose : To create emp_details table with 4 rows
 ******************************************************************/
function createTable(dbId) {
    var sqlStatement = "DROP TABLE IF EXISTS emp_details";
    kony.db.executeSql(dbId,
        sqlStatement,
        null,
        success_dropTable,
        commonErrorCallback);

    var sqlStatement = "CREATE TABLE IF NOT EXISTS emp_details (empid REAL PRIMARY KEY,empname TEXT,depid REAL)";
    kony.db.executeSql(dbId,
        sqlStatement,
        null,
        success_createTable,
        commonErrorCallback);
}



/*****************************************************************
Name    : commonErrorCallback
Purpose : To display error message on the console
 ******************************************************************/
function commonErrorCallback(transactionId, error) {
    kony.print(" Error code:: " + error);
    kony.print(" Error message:: " + error.message);
}


/*****************************************************************
Name    : commonVoidcallback
Purpose : To display success message on the console.
 ******************************************************************/
function commonVoidcallback() {
    kony.print("The transaction was executed successfully.");
}


/*****************************************************************
Name    : success_dropTable
Purpose : To display a message on the console when table creation is unsuccessful.
 ******************************************************************/
function success_dropTable(transactionId, resultset) {
    kony.print("Table was dropped");
}


/*****************************************************************
  Name    : Transaction
  Purpose : To open a transaction on click of Add button.
 ******************************************************************/
function transaction() {
    baseObjectId = kony.db.openDatabase("webSqlDB",
        "1.0",
        "Sample SQL Database",
        5 * 1024 * 1024);

    kony.db.transaction(baseObjectId, insertTable, commonErrorCallback, commonVoidcallback);
}


/*****************************************************************
  Name    : insertTable
  Purpose : To insert row in the table
 ******************************************************************/
function insertTable(dbId) {
    var empID = MainForm.IDTextBox.text;
    var empName = MainForm.NameTextBox.text;
    var depID = MainForm.DepartmentTextBox.text;
    var sqlStatement = "INSERT INTO emp_details VALUES (" + empID + ",\"" + empName + "\"," + depID + ")";

    kony.db.executeSql(dbId,
        sqlStatement,
        null,
        success_insertTable,
        commonErrorCallback);


    var sqlStatement = "SELECT * FROM emp_details";
    kony.db.executeSql(dbId,
        sqlStatement,
        null,
        success_sqlSelect,
        commonErrorCallback);

    ListForm.show();
}
/*****************************************************************
  Name    : success_insertTable
  Purpose :alert of successfully inserted table
 ******************************************************************/

function success_insertTable(transactionId, resultset) {
    kony.print("Insert is done successfully");
}


/*****************************************************************
  Name    : success_sqlSelect
  Purpose : To show all the rows of 'emp_details' table in the form
 ******************************************************************/
function success_sqlSelect(transactionId, resultset) {
    var dataObj1;
    //logic to process the resultset
    for (var i = 0; i < resultset.rows.length; i++) {
        var rowItem = kony.db.sqlResultsetRowItem(transactionId, resultset, i);
        kony.print(" empname:" + rowItem.empname);

        dataObj1 = {
            empID: rowItem.empid,
            empName: rowItem.empname,
            depID: rowItem.depid,
        };



        ListForm.Segment01.addDataAt(dataObj1, i);
    }
}
/*****************************************************************
  Name    : success_createTable
  Purpose : To display a message when table creation is successful.
 ******************************************************************/

function success_createTable(transactionId, resultset) {
    kony.print("Table is created successfully");
}

1 comment:

  1. Very useful document to start with SqlLite to store offline objects without sync.

    ReplyDelete