0

在我的应用程序中,我想在表中插入值。我在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

请帮忙..

4

2 回答 2

1

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

于 2012-07-30T05:29:30.880 回答
1

Arindam,不要忘记默认情况下 sql 的执行是异步的。因此,没有人保证您的 sql 将按照您在函数 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");
         });
     });
}

干杯,奥列格

于 2012-07-28T14:15:39.433 回答