日期:2014-05-16  浏览次数:20468 次

本地数据库存储JS类

//js调用本地数据库主JS文件
var HD = {
	is_db : null,
	uid : null,
	ukey : null,
	shortName : 'hd',
	version : '1.0',
	displayName : 'hd_2345',
	maxSize : 100000,
	data : null,
	other : null,
	login : null,
	index : null,
	// 创建数据库
	initDatabase : function() {
		try {
			if (!window.openDatabase) {
				alert('Databases are not supported in this browser.');
			} else {
				DEMODB = openDatabase(this.shortName, this.version,
						this.displayName, this.maxSize);
				this.createUserTable();
				this.createMyWebSite();
				this.createMyApp();
				this.createCitySite();
				this.createMyFenlei();
			}
		} catch (e) {
			if (e == 2) {
				// Version number mismatch.
			} else {
			}
			return;
		}
	},
	createUserTable : function() {
		DEMODB
				.transaction(function(transaction) {
					var sql = "CREATE TABLE IF NOT EXISTS hd_users("
							+ "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
							+ "uid INTEGER NOT NULL,passid INTEGER NOT NULL,uname TEXT NOT NULL,ukey TEXT NOT NULL,"
							+ "uemail TEXT NULL,city TEXT NULL)";
					transaction.executeSql(sql, [], HD.nullDataHandler,
							HD.errorHandler);
				});
	},
	createMyWebSite : function() {
		DEMODB.transaction(function(transaction) {
			var sql = "CREATE TABLE IF NOT EXISTS hd_users_website("
					+ "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
					+ "uid INTEGER NOT NULL,content TEXT NULL)";
			transaction
					.executeSql(sql, [], HD.nullDataHandler, HD.errorHandler);
		});
	},
	createMyApp : function() {
		DEMODB.transaction(function(transaction) {
			var sql = "CREATE TABLE IF NOT EXISTS hd_users_app("
					+ "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
					+ "uid INTEGER NOT NULL,content TEXT NULL)";
			transaction
					.executeSql(sql, [], HD.nullDataHandler, HD.errorHandler);
		});
	},
	createMyFenlei:function(){
		DEMODB.transaction(function(transaction) {
			var sql = "CREATE TABLE IF NOT EXISTS hd_users_fenlei("
					+ "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
					+ "uid INTEGER NOT NULL,content TEXT NULL)";
			transaction
					.executeSql(sql, [], HD.nullDataHandler, HD.errorHandler);
		});		
	},
	createCitySite : function() {
		DEMODB.transaction(function(transaction) {
			var sql = "CREATE TABLE IF NOT EXISTS hd_city_site("
					+ "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
					+ "uid INTEGER NULL,content TEXT NULL)";
			transaction
					.executeSql(sql, [], HD.nullDataHandler, HD.errorHandler);
		});
	},
	errorHandler : function(transaction, error) {
		// Error is a human-readable string.
		return false;
	},
	nullDataHandler : function() {
	},
	insert : function(data, table_name) {
		DEMODB.transaction(function(transaction) {
			var sql_addsub_name = '';
			var sql_add_var_name = "";
			for ( var k in data) {
				sql_addsub_name += " `" + k + "` ,";
				sql_add_var_name += " '" + data[k] + "' ,";
			}

			sql_addsub_name = sql_addsub_name.substr(0,
					(sql_addsub_name.length - 1));
			sql_add_var_name = sql_add_var_name.substr(0,
					(sql_add_var_name.length - 1));
			var sqlAdd = "INSERT INTO " + table_name + " ( " + sql_addsub_name
					+ ") VALUES (" + sql_add_var_name + ")";
			// 输出sql语句
			transaction.executeSql(sqlAdd, [], HD.nullDataHandler,
					HD.errorHandler);
		});
	},
	update : function(where, data, table_name) {
		DEMODB.transaction(function(transaction) {
			var sql = "";
			var edit_sql = "";
			for ( var k in data) {
				edit_sql += "`" + k + "` = " + "'" + data[k] + "' ,";
			}
			edit_sql = edit_sql.substr(0, (edit_sql.length - 1));
			sql = "UPDATE `" + table_name + "` SET " + edit_sql + " WHERE "
					+ where;
			transaction
					.executeSql(sql, [], HD.nullDataHandler, HD.errorHandler);
		});
	},
	deletedata : function(table_name, where) {
		DEMODB.transaction(function(transaction) {
			var sqlDel = "DELETE FROM " + table_name + " WHERE " + where;
			transaction.executeSql(sqlDel, [], HD.nullDataHandler,
					HD.errorHandler);
		});
	},
	deleteall : function(ta