Win an Ipad
  Developer   Documents   Data-set

Data-set

This article explains basics Data-set operation in Moscrif. Data-set is an object returned using database execution method.

Data-set


Dataset instance is a result of SQLite database execution method. Instance is created automatically and if returned false, an error occurs. Dataset is a record, function Dataset.next gives next dataset or end of dataset.

There is generic Dataset.name method to get name of the field, table or database.

See table and example below how to use this feature. The name of the field is returned as default. Second parameter has following symbols:

#field returns name of the current field
#table returns name of the table
#database returns name of the database

 

In this example, we re-use database resource from previous example:

//create SQL query
var result = database.exec("select * from users"); //name of second column, “firstName”
console< console<

If we want to work in application that required loaded data, data-set
does not fit our requirements. Let’s create a function which gives us a JSON object as result. Expected format of object is { field : value, … }


Example data-set to JSON:

// Returns object constructed from current row as { field1: value1, field2: value2, etc. }
Dataset.rowAsObject = function ()
{
    var names = this.names;
    if (!names)
    {
        names = [];
    for (var n = 0; n < this.length; ++n)
        names.push(symbol(this.name(n)));
    this.names = names;
              }
    var obj = {};
    var n = 0;
    for (var v in this)
    {
        obj[names[n++]] = v;
    }
    return obj;
}

 Data-set 
Write a Comment (0)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.