0

这个库应该易于使用来访问 html5 本地存储(sqlite)。像定义表,添加/更新/删除实体,从数据库查询实体。

可能使用 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);
4

1 回答 1

0

我正在使用包含 html5 sqlite 的 DAL 部分的 phonegap 库。它由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---------------------------------------------------- 
于 2013-01-11T01:35:18.807 回答