4

我有一个使用phonegap编写的应用程序,我想在我的应用程序第一次加载时创建表和数据并在数据库中插入,但是每次运行我的应用程序时,再次插入数据,加载时间增加,如果表创建了如何检查不再插入数据?

    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");
}
4

1 回答 1

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);
    }
}
于 2013-07-10T12:47:08.497 回答