在phonegap数据库表中插入数据?

时间:2012-07-28 07:25:54

标签: android cordova sencha-touch sencha-touch-2

在我的应用程序中,我想在表中插入值。我在 index.html 文件中的 onDeviceReady()方法中创建了数据库。

     <!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>

现在我有一个注册屏幕,在注册屏幕中我有提交按钮。因此,如果我单击提交按钮,所有细节都应该存储在DEMO表中的数据库中。所以在控制器中,我正在编写按钮事件,

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

但是它给了我警报,没有插入数据。 我点击了链接:Phonegap DB Link

请帮助..

2 个答案:

答案 0 :(得分:1)

Arindam,不要忘记默认执行sql是异步的。因此,无法保证您的sqls将按照您在函数populateDB(tx)中定义的顺序执行。例如。它可以先执行创建而不是删除...

为了确保您必须在回调函数中定义后续调用

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

干杯,奥列格

答案 1 :(得分:1)

使用Cordova 2.0.0,您的问题将得到解决..您将能够在文件资源管理器中看到您创建的数据库。如果有帮助,请告诉我。