是否有免费的lib访问html5数据库SQLite?

时间:2013-01-10 14:26:45

标签: html5 sqlite cordova

这个lib应该易于使用来访问html5本地存储(sqlite)。与定义表一样,添加/更新/删除实体,从db。中查询实体。

与lib一样,我可以编写如下代码:

//代码

var db=new MyDataBase();
var users = db.Users.Tolist();
var admin = db.Users.FirstOrDefault(u=>u.Name=="admin");
admin.Password="new password";
db.UpdateUser(admin);

1 个答案:

答案 0 :(得分:0)

我正在使用一个包含html5 sqlite的DAL部分的phonegap lib。它由novasoftware开发。看看它的示例代码here,我也将其中的一部分粘贴在下面:

/// <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----------------------------------------------------