javascript如何讀寫(xiě)本地sqlite數(shù)據(jù)庫(kù)
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)文章
JS根據(jù)年月獲得當(dāng)月天數(shù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了JS根據(jù)年月獲得當(dāng)月天數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-07-07
require.js配合插件text.js實(shí)現(xiàn)最簡(jiǎn)單的單頁(yè)應(yīng)用程序
這篇文章主要介紹了require.js配合插件text.js實(shí)現(xiàn)最簡(jiǎn)單的單頁(yè)應(yīng)用程序,需要的朋友可以參考下2016-07-07
JS頁(yè)面獲取 session 值,作用域和閉包學(xué)習(xí)筆記
這篇文章主要介紹了JS頁(yè)面獲取 session 值,作用域和閉包,結(jié)合具體實(shí)例形式分析了javascript與jsp交互獲取session值、函數(shù)作用域及閉包相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
給localStorage設(shè)置一個(gè)過(guò)期時(shí)間的方法分享
我們都知道localStorage不主動(dòng)刪除,永遠(yuǎn)不會(huì)銷(xiāo)毀,那么如何設(shè)置localStorage的過(guò)期時(shí)間呢?下面這篇文章主要給大家介紹了關(guān)于如何給localStorage設(shè)置一個(gè)過(guò)期時(shí)間的相關(guān)資料,需要的朋友可以參考下2018-11-11
javascript 緩沖效果 實(shí)現(xiàn)代碼
非常漂亮的緩沖效果代碼,大家可以看看下。2009-06-06

