Insert the data in phonegap database table?

允我心安 提交于 2019-12-05 07:54:25

问题


In my application, i want to insert the value in the table. i have created the databse in onDeviceReady() method in the index.html file.

     <!DOCTYPE html>
<!-- Auto Generated with Sencha Architect -->
<!-- Modifications to this file will be overwritten. -->
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Teritree</title>
<script type="text/javascript" id="phonegap" src=
"cordova-2.0.0.js">
</script>

<script type="text/javascript" src="barcodescanner.js">
</script>

<script src="sencha-touch-all.js" type="text/javascript">
</script>

<link rel="stylesheet" type="text/css" href="app.css">
<script type="text/javascript" src="app/Ext.ux.touch.Rating.js">
</script>

<script type="text/javascript" src="app/app.js">
</script>

<script type="text/javascript">
        Ext.Loader.setConfig({ disableCaching: false });
        Ext.Ajax.setDisableCaching(false);    

</script>

<script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value 
    var mydb;

    // Wait for PhoneGap to connect with the device
    function onLoad() {
        document.addEventListener("deviceready",onDeviceReady,false);
    }

    // PhoneGap is ready to be used!

    function onDeviceReady() {
        console.log("it is in ondevice ready method..");
        mydb = window.openDatabase("teritreeDb", "1.0", "TestDB", 10000);
        console.log("it is in ondevice ready method..1");
        mydb.transaction(populateDB, errorCB, successCB);
        console.log("it is in ondevice ready method..2");
   }

   function populateDB(tx) {
   console.log("it is in populateDB----------1");
     tx.executeSql('DROP TABLE IF EXISTS DEMO', function () {
         tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)', function () {
             console.log("Table created");
         });
     });

      // Transaction error callback
    //
    function errorCB(tx, err) {
        alert("Error processing SQL: "+err);
    }

    // Transaction success callback
    //
    function successCB() {
        alert("success!");
    }
}

</script>

<script type="text/javascript">
        if (!Ext.browser.is.WebKit) {
            alert("The current browser is unsupported.\n\nSupported browsers:\n" +
                "Google Chrome\n" +
                "Apple Safari\n" +
                "Mobile Safari (iOS)\n" +
                "Android Browser\n" +
                "BlackBerry Browser"
            );
        }

</script>
</head>
<body>
</body>
</html>

Now i have one signup screen and there in the signup screen i have submit button. So if i click the submit button, all the details should store in the database means in the DEMO table..So in the controller, i am writing the button event,

     onSubmitButtonTap : function(button, e, options) {

        mydb = window.openDatabase("appDb", "1.0", "Test DB", 1000000);
        mydb.transaction(storeCustomerDetails, error, success);


    function storeCustomerDetails(tx)
    {
        tx.executeSql('insert into DEMO (id,data) values("1","Hello")');
        tx.executeSql('insert into DEMO (id,data) values("2","Hello World")');
    }

    function error(){
        alert("data is not inserted");
    }
    function success(){
        alert("data is succesfully inserted");
    }
},

but it is giving me the alert, data is not inserted. I followed the link: Phonegap DB Link

Please help..


回答1:


Use Cordova 2.0.0 and your issue will be solved.. You will be able to see the database you created in the file explorer. Do let me know if it helps you.




回答2:


Arindam, do not forget by default the executing of the sql is asyncronious. Therefore nobody warranty your sqls will be executed in order you have defined in function populateDB(tx). Eg. it could execute first create than delete...

To be sure in the order you have to define subsequent calls in the callback function

function populateDB(tx) {
     tx.executeSql('DROP TABLE IF EXISTS DEMO', function () {
         tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)', function () {
             console.log("Table created");
         });
     });
}

Cheers, Oleg



来源:https://stackoverflow.com/questions/11699165/insert-the-data-in-phonegap-database-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!