javascript如何讀寫本地sqlite數(shù)據(jù)庫
javascript讀寫本地sqlite數(shù)據(jù)庫
sqlite這種單文件數(shù)據(jù)庫,類型簡單功能強大效率也不錯,非常適合單機軟件開發(fā)。
把一個我以前寫的JavaScript sqlite數(shù)據(jù)庫操作類分享給大家,還是先上代碼,注釋寫的很清楚啦,支持增刪改查,支持鏈式查詢,使用的時候不用new。
/*sqlite數(shù)據(jù)庫操作類 by sdxjwkq01*/ this.Db={ tableName:"",//表 whereReg:"",//where條件 orderReg:"",//排序條件 pageReg:"",//分頁 dbUrl:"DRIVER=SQLite3 ODBC Driver;Database=Db/database.db",//數(shù)據(jù)庫地址 //取得表 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; }, //分頁 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(); }, //查詢 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ù)
也可以像下圖這樣直接運行sql語句
運行這個sqlite操作類,電腦需要安裝SQLite ODBC 驅(qū)動,非精簡版系統(tǒng)一般都有安裝,這個步驟可以忽略。
javascript直接操作sqlite數(shù)據(jù)庫demo
朋友問我瀏覽器js直接sqlite怎么做。。。?
我一臉的懵逼。。。啥是sqlite。。。。?
然后各種查資料。。。終于有了這個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>宇宙已無對手的Demo演示 --- 功能強非常之大的評分 + 數(shù)據(jù)存儲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">請選擇評分</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ù)庫教程: ? ? <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> ? ? 星級評分: ? ? <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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
javascript設(shè)計模式 – 模板方法模式原理與用法實例分析
這篇文章主要介紹了javascript設(shè)計模式 – 模板方法模式原理,結(jié)合實例形式分析了javascript模板方法模式相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04JavaScript實現(xiàn)圖片滑動切換的代碼示例分享
這篇文章主要介紹了JavaScript實現(xiàn)圖片滑動切換的代碼示例分享,能夠控制包括滑動時間和切換數(shù)量等,需要的朋友可以參考下2016-03-03談?wù)勎覍avaScript原型和閉包系列理解(隨手筆記9)
這篇文章主要介紹了談?wù)勎覍avaScript原型和閉包系列理解(隨手筆記9)的相關(guān)資料,需要的朋友可以參考下2015-12-12JavaScript實現(xiàn)在標題欄上顯示當前日期的方法
這篇文章主要介紹了JavaScript實現(xiàn)在標題欄上顯示當前日期的方法,涉及javascript操作時間及DOM節(jié)點的技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03JS獲取表格內(nèi)指定單元格html內(nèi)容的方法
這篇文章主要介紹了JS獲取表格內(nèi)指定單元格html內(nèi)容的方法,涉及javascript中innerHTML屬性的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03十個優(yōu)秀的Ajax/Javascript實例網(wǎng)站收集
今天,要向大家推薦10個相當棒的Ajax和Javascript國外資源網(wǎng)站或博客,它們提供了相當多的高質(zhì)量Ajax、Javascript實例及教程,喜歡Ajax和Javascript的朋友絕對不能錯過。2010-03-03根據(jù)身份證號自動輸出相關(guān)信息(籍貫,出身日期,性別)
為了減少客戶的在頁面的輸入,做了這個效果,他可以根據(jù)用戶輸入的身份證號輸出籍貫、出身日期、性別的相關(guān)信息,需要的朋友可以參考下2013-11-11