2016-07-27 8 views
0

私のcordovaプロジェクトでmysql liteデータベースに問題があります。 私はhtc one x + androidバージョン4.2.2でテストしようとしていますが、何も動作しません。 javascriptアラートとデータベースコードの両方が電話機とエミュレータで機能しません。奇妙なことは、戻るボタンのコードは機能しますが、このコードの警告は機能しません。理由を見つけることができますか?ここにコードが来る。mysql lite database cordova

 <script type="text/javascript" charset="utf-8"> 
     function onBackButton() { 
      console.log("Test"); 
      alert("Back Button works!"); 
      window.location = "bearbeiten.html"; 
     } 
     function onLoad() { 
      alert("onLoad method works!"); 
      document.addEventListener("deviceready", onDeviceReadyBb, false); 
     } 
     function onDeviceReadyBb(){ 
      document.addEventListener("backbutton", onBackButton, false); 
     } 
     function saveTopic(){ 
      //Load the device API libraries 
      document.addEventListener("deviceready", onDeviceReady, false); 
     }// Device APIs are now available 
     function onDeviceReady() { 
      //Create database object 
      var db = window.openDatabase("Database", "1.0", "EasyLearning", 200000); 
      db.transaction(populateDB, errorCB, successCB); 
     } 
     //Create database table "topics" 
     function populateDB(tx){ 
      var text = document.getElementById("t1").value; 
      alert(text+" is saved in database!"); 
      var abfrage = tx.executeSql('SELECT COUNT(*) FROM themen WHERE thema ='+text+'); 
      if(abfrage == 1) 
      { 
       alert("topic is alredy existing!"); 
      } 
      else{ 
       alert("New topic will be saved...") 
       tx.executeSql('CREATE TABLE IF NOT EXISTS themen (themennr INTEGER AUTO_INCREMENT, thema VARCHAR(10000)'); 
       tx.executeSql('INSERT INTO themen(thema) VALUES ('+text+')'); 
      } 
     } 
     //Shows error if the query is wrong 
     function errorCB(tx, err) { 
      alert("Error processing SQL: "+err); 
     } 
     //Shows successful message if it works 
     function successCB() { 
      alert("Thema erfolgreich gespeichert!"); 
     } 
     </script> 
+0

これは単なる関数定義の集まりです。あなたはこれらの機能を呼び出すことはないようです。 –

答えて

1

まずはSQLiteではなく、使用しているWebSQLです。

私はそれの構造を変更し、コルドバのドキュメントで推奨されるベストプラクティスの方法でそれをやった、と私はあなたの問題がそれを見てとることで解決されることを願っていますあなたのコードに問題がありました:

var app = { 
      initialize: function() { 
      this.bindEvents(); 
     }, 
     bindEvents: function(){ 

      document.addEventListener('deviceready', this.onDeviceReady, false); 
      document.addEventListener('backbutton', this.onBackButton, false); 

     }, 
     onBackButton: function(){ 
      console.log("Test"); 
      alert("Back Button works!"); 
      window.location = "bearbeiten.html"; 
     }, 
     onDeviceReady: function() { 
      var db = window.openDatabase("Database", "1.0", "EasyLearning", 200000); 
      db.transaction(populateDB, errorCB, successCB); 

     }, 
     populateDB: function(tx){ 
      var text = document.getElementById("t1").value; 
      alert(text+" is saved in database!"); 
      var abfrage = tx.executeSql('SELECT COUNT(*) FROM themen WHERE thema ='+text+'); 
      if(abfrage == 1) 
      { 
       alert("topic is alredy existing!"); 
      } 
      else{ 
       alert("New topic will be saved...") 
       tx.executeSql('CREATE TABLE IF NOT EXISTS themen (themennr INTEGER AUTO_INCREMENT, thema VARCHAR(10000)'); 
       tx.executeSql('INSERT INTO themen(thema) VALUES ('+text+')'); 
      } 
     }, 
     errorCB: function(tx, err) { 
      alert("Error processing SQL: "+err); 
     }, 
     //Shows successful message if it works 
     successCB: function() { 
      alert("Thema erfolgreich gespeichert!"); 
     } 
    } 
    app.initialize(); 
は、

私はあなたの関数の本体を変更していないことに注意してください、私はそれが実行される方法で書きました。 携帯端末でテストする前に、ブラウザコンソールを使用してコードをデバッグすることを忘れないでください。

0

最初にお返事ありがとうございます。

ただし、変更されたコードはデバイスでも機能しません。 ここで私はこのHTMLページの全コードを(あなたの変更を伴って)持っているかもしれません。

<html> 
<head> 
    <!-- 
    Customize this policy to fit your own app's needs. For more guidance, see: 
     https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy 
    Some notes: 
     * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication 
     * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly 
     * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: 
      * Enable inline JS: add 'unsafe-inline' to default-src 
    --> 
    <meta name="Autor" content="***************"> 
    <meta name="format-detection" content="telephone=no"> 
    <meta name="msapplication-tap-highlight" content="no"> 
    <!--viewport sorgt dafür das es nicht wie auf dem Desktop PC angezeigt wird--> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
    <link rel="stylesheet" type="text/css" href="css/neu.css"> 
    <script type="text/javascript" charset="utf-8"> 
    var app = { 
     initialize: function() { 
     this.bindEvents(); 
    }, 
    bindEvents: function(){ 

     document.addEventListener('deviceready', this.onDeviceReady, false); 
     document.addEventListener('backbutton', this.onBackButton, false); 

    }, 
    onBackButton: function(){ 
     console.log("Test"); 
     alert("Back Button works!"); 
     window.location = "bearbeiten.html"; 
    }, 
    onDeviceReady: function() { 
     var db = window.openDatabase("Database", "1.0", "EasyLearning", 200000); 
     db.transaction(populateDB, errorCB, successCB); 

    }, 
    populateDB: function(tx){ 
     var text = document.getElementById("t1").value; 
     alert(text+" is saved in database!"); 
     var abfrage = tx.executeSql('SELECT COUNT(*) FROM themen WHERE thema ='+text+'); 
     if(abfrage == 1) 
     { 
      alert("topic is alredy existing!"); 
     } 
     else{ 
      alert("New topic will be saved...") 
      tx.executeSql('CREATE TABLE IF NOT EXISTS themen (themennr INTEGER AUTO_INCREMENT, thema VARCHAR(10000)'); 
      tx.executeSql('INSERT INTO themen(thema) VALUES ('+text+')'); 
     } 
    }, 
    errorCB: function(tx, err) { 
     alert("Error processing SQL: "+err); 
    }, 
    //Shows successful message if it works 
    successCB: function() { 
     alert("Thema erfolgreich gespeichert!"); 
    } 
} 
app.initialize(); 
    </script> 
    <title>Easy Learning</title> 
</head> 
<body onload="onLoad()"> 
    <div class="app"> 
     <div id="deviceready"> 
      <p class="event listening"><span class="blink">Connecting to Device</span></p> 
      <div class="event received"> 
      <!-- your code --> 
      <!-- your code --> 
      </div> 
     </div> 
    </div> 
    <form action="javascript:void(0);"> 
    <textarea id="t1" placeholder="Themenbereich"></textarea> 
    <input type="submit" name="hinzu" id="b1" onclick="saveTopic()"> 
    </div> 
    </form> 
    <div id="bimg"><img id="bgimg" src="img\***********.png" alt="fehler" height="15%" width="100%"></div> 
    <script type="text/javascript" src="cordova.js"></script> 
    <script type="text/javascript" src="js/meinjs.js"</script> 
    <script type="text/javascript" src="js/index.js"></script> 
</body> 

0

私はそれはあなたの問題の理由であるかどうかわからないですが、あなたはそれを作成する前に、DBに問い合わせをお願いします。選択クエリの前にCreateクエリを配置します。 答えがあなたに役立つことを願っています。

+0

回答ありがとうございますが、結果に変更はありません。 –

+0

誰かが問題に対して別の答えを持っていますか? –

+0

コードバのダイアログAPIを試しましたか?おそらく、これはjsアラートより優れています。ここにリンクはありません:https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-dialogs/#installation – BenHeid