首次加载应用程序时,在phonegap中的sqlite数据库中插入数据

时间:2013-06-19 08:44:47

标签: android sqlite cordova

我有一个带phonegap的应用程序写入,我想第一次将我的应用程序加载,表和数据创建并插入数据库时​​,但每次运行我的应用程序时,再次插入数据,加载时间增加,怎么可以chek如果创建的表不再插入数据?

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

1 个答案:

答案 0 :(得分: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);
    }
}