Skip to content Skip to sidebar Skip to footer

Is There A Free Lib Accessing To Html5 Database Sqlite?

This lib should be easy to use to access to html5 local storage (sqlite). Like define tables, add/update/delete entity, query entities from db. Likely with the lib I can write code

Solution 1:

I am using a phonegap lib that contains the DAL part for html5 sqlite. It's developed by novasoftware. Take a look at it's sample code here, I also paste part of it below:

/// <reference path="scripts/novas/data/ArrayExtensions.js" />
/// <reference path="scripts/novas/data/nova.data.Repository.js" />
/// <reference path="scripts/novas/data/nova.data.DbContext.js" />
/// <reference path="scripts/novas/data/nova.data.Entity.js" />
/// <reference path="scripts/novas/data/nova.data.Queryable.js" />

// define dbContext & entities------------------------------------
var DemoDataContext = function () {
    nova.data.DbContext.call(this, "Demo", "1.0", "Demo DB", 1000000);

    this.users = new nova.data.Repository(this, User, "users");
    this.roles = new nova.data.Repository(this, Role, "roles");
};

DemoDataContext.prototype = new nova.data.DbContext();
DemoDataContext.constructor = DemoDataContext;

var User = function () {
  nova.data.Entity.call(this);
  this.name = "";
  this.password = "";
  this.birthYear = 1980;
  this.createdDate = new Date();
  this.deleted = false;
};
User.prototype = new nova.data.Entity();
User.constructor = User;

var Role = function () {
nova.data.Entity.call(this);
this.name = "";
this.createdDate = new Date();

};
Role.prototype = new nova.data.Entity();
Role.constructor = Role;
// end define dbContext & entities------------------------------------

// service methods----------------------------------------------------
function getAllUsers(callback) {
new DemoDataContext().users.toArray(function (users) {
    alert(users.length);
    callback(users);
});
}

function getUserByName(name, callback) {
  new DemoDataContext().users.where("name='" + name + "'").toArray(function (users) {
    callback(users.firstOrDefault());
  });
}

function addRole(roleName, callback) {
  var role = new Role();
  role.name = roleName;
  var db = new DemoDataContext();
  db.roles.add(role);
  db.saveChanges(callback);
}

function updateUserPassword(username, password, callback) {
  getUserByName(username, function (user) {
    if (user == null) {
        throw "no user found.";
    }
    user.password = password;
    var db = new DemoDataContext();
    db.users.update(user);
    db.saveChanges(callback);
  });
}

function deleteUserByName(name, callback) {
  getUserByName(name, function (user) {
    if (user == null) {
        throw "no user found.";
    }
    var db = new DemoDataContext();
    db.users.remove(user);
    db.saveChanges(callback);
  });
}

// end service methods---------------------------------------------------- 

Post a Comment for "Is There A Free Lib Accessing To Html5 Database Sqlite?"