2013-06-19 44 views
2

我有一個應用程序用phonegap編寫,我希望當我的應用程序加載時,表和數據創建並插入數據庫,但每次運行我的應用程序時,插入數據再次完成,加載時間增加,如果表創建不再插入數據,怎麼辦?在第一次加載應用程序時在phonegap中的sqlite數據庫中插入數據

document.addEventListener("deviceready", onDeviceReady, false); 


    var dbShell ; 

    function onDeviceReady() { 

    dbShell = window.openDatabase("BaharNarenj", "1.0", "BaharNarenj", 2000000); 

    dbShell.transaction(setupTable,dbErrorHandler,getEntries); 

    } 

function setupTable(tx){ 

    tx.executeSql("CREATE TABLE IF NOT EXISTS amaken(id INTEGER,title,des,px,py)");  

tx.executeSql('insert into amaken(id,title,des,px,py) values(2,"test","dec","36.566185","55.059502")');  

tx.executeSql('insert into amaken(id,title,des,px,py) values(4,"test5","dec5","36.566185","55.059502")'); 
} 



function dbErrorHandler(err){ 
alert("DB Error: "+err.message + "\nCode="+err.code); 
} 


function getEntries() { 
alert("done"); 
} 
+0

電話中沒有SharedPreference? –

回答

3

您可以設置一個標誌(如XApp1.0在下面的代碼)在本地存儲和檢查標誌的值,而隨後的應用程序的運行。希望這會幫助你。

function onDeviceReady() { 
    var firstrun = window.localStorage.getItem("XApp1.0"); 
    if (firstrun == null) { 
     window.localStorage.setItem("XApp1.0", "1"); 
     var db = window.openDatabase("XApp", "1.0", "XApp", 200000); 
     db.transaction(populateDB, errorCB, successCB); 
    } 
    else { 
     // Db Alredy Exists 
     var db = window.openDatabase("XApp", "1.0", "XApp", 200000); 
     db.transaction(queryDB, errorCB); 
    } 
} 
相關問題