2012-07-28 59 views
0

在我的應用程序中,我想要在表中插入值。我在index.html文件中創建了onDeviceReady()方法中的數據庫。將數據插入phonegap數據庫表中?

 <!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 table..So意味着在控制器中,我寫的按鈕事件,

 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

請幫助..

+0

你使用哪個Phonegap版本? – 2012-07-28 10:31:18

+0

corodova-1.8.1版本 – 2012-07-28 10:41:45

+0

我在瀏覽器..db中創建了..但表格沒有創建 – 2012-07-28 10:54:19

回答

1

使用科爾多瓦2.0.0您的問題將得到解決..您將能夠看到您在文件資源管理器中創建的數據庫。請讓我知道它是否可以幫助你。

+0

Ok.lets see ..thanks .. – 2012-07-30 05:31:33

+0

添加2.0之後,我得到了問題..無法解析Lorg/apache/cordova/CordovaWebViewClient中的新實例159(Landroid/webkit/WebResourceResponse;); – 2012-07-30 07:00:05

+0

多數民衆贊成在沒有問題...它會來....離開那... – 2012-07-30 07:00:50

1

阿瑞丹姆,默認情況下不會忘了SQL的執行是asyncronious。因此,沒有人保證您的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"); 
     }); 
    }); 
} 

乾杯,奧列格

+0

我試過了..但仍然沒有運氣 – 2012-07-29 08:01:28

+0

問題是,我無法看到eclipse中的文件資源管理器中的數據庫。並且甚至沒有在平板SD卡上。 – 2012-07-29 09:16:41

+0

什麼意思沒有運氣?我的方案中是否收到「表格創建」消息? – olegtaranenko 2012-07-29 20:29:56