2013-01-10 54 views
0

這個庫應該很容易用來訪問html5本地存儲(sqlite)。像定義表一樣,添加/更新/刪除實體,從數據庫查詢實體。有免費的lib訪問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); 
+0

退房這樣的回答: http://stackoverflow.com/questions/1878256/html5-localstorage-sql –

回答

0

我使用一個包含HTML5源碼的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---------------------------------------------------- 
+0

獅子座,你是否必須先創建數據庫?我正在嘗試這個,但我得到的表不存在的錯誤。 – box86rowh

+0

不需要手動創建數據庫。當您新建dbcontext的實例時,會創建一個數據庫。該框架已經改變了垃圾,你可以檢查最新的源代碼。通常,我建議您添加一個訪問yourDbContext的dbservice.js。您也可以訂閱我們的源代碼,稍後我會更新文檔頁面。 –