亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript如何讀寫(xiě)本地sqlite數(shù)據(jù)庫(kù)

 更新時(shí)間:2023年02月27日 08:50:14   作者:sdxjwkq01  
這篇文章主要介紹了javascript如何讀寫(xiě)本地sqlite數(shù)據(jù)庫(kù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

javascript讀寫(xiě)本地sqlite數(shù)據(jù)庫(kù)

sqlite這種單文件數(shù)據(jù)庫(kù),類(lèi)型簡(jiǎn)單功能強(qiáng)大效率也不錯(cuò),非常適合單機(jī)軟件開(kāi)發(fā)。

把一個(gè)我以前寫(xiě)的JavaScript sqlite數(shù)據(jù)庫(kù)操作類(lèi)分享給大家,還是先上代碼,注釋寫(xiě)的很清楚啦,支持增刪改查,支持鏈?zhǔn)讲樵?xún),使用的時(shí)候不用new。

/*sqlite數(shù)據(jù)庫(kù)操作類(lèi) by sdxjwkq01*/
this.Db={
	tableName:"",//表
	whereReg:"",//where條件
	orderReg:"",//排序條件
	pageReg:"",//分頁(yè)
	dbUrl:"DRIVER=SQLite3 ODBC Driver;Database=Db/database.db",//數(shù)據(jù)庫(kù)地址
	//取得表
	table:function(tableName){
		this.tableName=tableName;
		return this;
	},
	//取得where
	where:function(whereReg){
		this.whereReg=whereReg;
		return this;
	},
	//排序
	order:function(orderReg){
		this.orderReg=orderReg;
		return this;
	},
	//分頁(yè)
	page:function(pageReg){
		this.pageReg=pageReg;
		return this;
	},
	//添加
	add:function(json){
		var sql="insert into "+this.tableName+"(";
		var fields=[];
		var values=[];
		for(var item in json){
			fields.push(item);
			values.push("'"+json[item]+"'");
		}
		sql+=fields.join(",");
		sql+=") values("+values.join(",")+")";
		var con = new ActiveXObject("ADODB.Connection");
		con.ConnectionString =this.dbUrl;
		con.Open();
		con.Execute(sql);
		con.Close();
	},
	//刪除
	del:function(id){
		var con = new ActiveXObject("ADODB.Connection");
		con.ConnectionString = this.dbUrl;
		con.Open();
		if(typeof id=="object"){
			con.Execute("delete from "+this.tableName+" where id in ("+id.join(",")+")");
		}else{
			con.Execute("delete from "+this.tableName+" where id="+id);
		}
		con.Close();
	},
	//修改
	upd:function(json){
		var sql="update "+this.tableName+" set ";
		var data=[];
		for(var item in json){
			data.push(item+"="+json[item]);
		}
		sql+=data.join(",");
		if(this.whereReg.length>0){
			sql+=" where "+this.whereReg;
		}
		var con = new ActiveXObject("ADODB.Connection");
		con.ConnectionString =this.dbUrl;
		con.Open();
		var re=con.Execute(sql);
		con.Close();
	},
	//查詢(xún)
	sel:function(){
		var con = new ActiveXObject("ADODB.Connection");
		con.ConnectionString =this.dbUrl;
		con.Open();
		var sql="";
		sql+="select * from "+this.tableName;
		if(this.whereReg.length>0){
			sql+=" where "+this.whereReg;
		}
		if(this.orderReg.length>0){
			sql+=" order by "+this.orderReg;
		}
		if(this.pageReg.length>0){
			var limit=this.pageReg.split(",");
			sql+=" limit "+limit[0]+" offset "+limit[1];
		}
		var result=con.Execute(sql);
		var resultArray=[];
		var h=0;
		while(!result.eof){
			if(h==0){
				//試探指針位置
				for(i=0;;i++){
					try{
						eval("var temp=result("+i+")");
					}catch(e){
						var fieldLength=i;
						break;
					}
				}
				h++;
			}
			var temp=[];
			for(i=0;i<fieldLength;i++){
				eval("temp.push(''+result("+i+"))");
			}
			resultArray.push(temp);
			result.movenext(); 
		}
		con.Close();
		return resultArray;
	},
	//直接執(zhí)行
	execute:function(sql){
		var con = new ActiveXObject("ADODB.Connection");
		con.ConnectionString =this.dbUrl;
		con.Open();
		var result=con.Execute(sql);
		var resultArray=[];
		var h=0;
		while(!result.eof){
			if(h==0){
				//試探指針位置
				for(i=0;;i++){
					try{
						eval("var temp=result("+i+")");
					}catch(e){
						var fieldLength=i;
						break;
					}
				}
				h++;
			}
			var temp=[];
			for(i=0;i<fieldLength;i++){
				eval("temp.push(''+result("+i+"))");
			}
			resultArray.push(temp);
			result.movenext(); 
		}
		con.Close();
		return resultArray;
	}
}

例如下面是更新一條數(shù)據(jù)

也可以像下圖這樣直接運(yùn)行sql語(yǔ)句

運(yùn)行這個(gè)sqlite操作類(lèi),電腦需要安裝SQLite ODBC 驅(qū)動(dòng),非精簡(jiǎn)版系統(tǒng)一般都有安裝,這個(gè)步驟可以忽略。

javascript直接操作sqlite數(shù)據(jù)庫(kù)demo

朋友問(wèn)我瀏覽器js直接sqlite怎么做。。。?

我一臉的懵逼。。。啥是sqlite。。。。?

然后各種查資料。。。終于有了這個(gè)demo。。。。

記錄下,后面可能用的到。。。。。

<html lang="en" dir="ltr">
<head>
? ? <meta charset="utf-8">
? ? <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
? ? <meta content="width=device-width; initial-scale=1; maximum-scale=1" name="viewport">
? ? <title>宇宙已無(wú)對(duì)手的Demo演示 --- 功能強(qiáng)非常之大的評(píng)分 + 數(shù)據(jù)存儲(chǔ)Sqlite Demo演示</title>
? ? <script type="text/javascript" src="lib/jquery.min.js"></script>
? ? <script type="text/javascript" src="lib/raty/jquery.raty.js"></script>
</head>
<body>
<div style="width:500px; margin:100px auto;">
? ? <div class="demo">
? ? ? ? <div style="padding-top: 20px;padding-bottom: 20px">主題:<input type="text" name="theme" id="theme"/></div>
?
? ? ? ? <div style="padding-top: 20px;padding-bottom: 20px ">
? ? ? ? ? ? <div id="starView"></div>
? ? ? ? ? ? <div id="function-hint" class="hint">請(qǐng)選擇評(píng)分</div>
? ? ? ? </div>
?
? ? ? ? <div style="padding-top: 20px ;padding-bottom: 20px">備注:<textarea id="remark" name="remark"></textarea></div>
? ? ? ? <button id="save">保存</button>
? ? ? ? <button id="read">讀數(shù)據(jù)</button>
?
?
? ? </div>
</div>
<div>
? ? windows安裝sqlite數(shù)據(jù)庫(kù)教程:
? ? <p>https://github.com/kripken/sql.js</p>
? ? <p>http://www.runoob.com/sqlite/sqlite-installation.html</p>
? ? <p>https://blog.csdn.net/chaishen10000/article/details/54574060</p>
? ? <p>https://blog.csdn.net/u012562302/article/details/78362465</p>
? ? 星級(jí)評(píng)分:
? ? <p>https://github.com/wbotelhos/raty</p>
? ? <p>http://www.shouce.ren/example/try?pc=/api/jq/5733e33070c5a/index.html</p>
? ? IE下使用Sqlite
? ? <p>https://blog.csdn.net/fhl812432059/article/details/51502724</p>
</div>
<script type="text/javascript" src="./ie.js"></script>
</body>
</html>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論