数据库无法在phonegap中打开

时间:2014-11-13 09:18:08

标签: android cordova android-sqlite

我正在尝试执行phonegap示例,它应该创建数据库并创建表。我想从下面的链接执行       http://docs.phonegap.com/en/1.4.1/phonegap_storage_storage.md.html#Storage

下面是我的代码

  <!DOCTYPE html>
  <html>
  <head>
  <title>Contact Example</title>
  <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
 <script type="text/javascript" charset="utf-8">
 function datab()
 {
  alert("i m in db");
  var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
  db.transaction(function (tx) {
  tx.executeSql('DROP TABLE IF EXISTS LOGSS');
  tx.executeSql('CREATE TABLE IF NOT EXISTS file1 (id unique, log1 , log2)');
  tx.executeSql('INSERT INTO file1 (id, data) VALUES (1, "First row")');
  });
  db.transaction(function (tx) {
  tx.executeSql('SELECT * FROM file1', [], function (tx, results) {
  var len = results.rows.length, i , j;
  for (i = 0; i < len; i++){
  alert(results.rows.item(i).log1);
  alert(results.rows.item(i).log2);
  }
  }, null);
  }); 
  }
  </script>
  </head>
  <body>
  <h1>Example</h1>
  <p>Database</p>
  <input type="button" onclick="datab();">
  </body>
  </html>

执行此代码时,未定义生成错误openDatabase。我已经包含了phonegap jar文件。我在做什么错?请有人帮忙。

1 个答案:

答案 0 :(得分:0)

您正在使用哪种Cordova版本?
您提到的链接中的cordova版本很旧(1.4)
目前的cordova版本是4.0

您需要使用window.openDatabase代替openDatabase。您还需要在调用数据库函数之前等待deviceReady事件。

var db = window.openDatabase

有关详细信息,请参阅链接here

编辑1: 请查看以下链接以安装cordova。

http://thejackalofjavascript.com/phonegap-3-cli-setup-mac-windows/

http://coenraets.org/blog/cordova-phonegap-3-tutorial/

http://teusink.blogspot.in/2013/07/guide-phonegap-3-android-windows.html

http://sdk.revmobmobileadnetwork.com/phonegap_cordova.html

http://docs.phonegap.com/en/3.5.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide

Phonegap Cordova installation Windows

PhoneGap 3.1.0 without node / npm and command line tools

相关问题