如何在PhoneGap中从SD卡读取数据库

时间:2013-06-14 13:13:02

标签: javascript android cordova

我是Phonegap的新手。我想从SDcard读取数据库文件。我通过SQLite浏览器创建了一个“演示”数据库,然后将其放入SD卡。现在我想从SDcard中读取它。我使用以下代码,但它无法正常工作。

     function save_address(name) {

 var db;
 db = window.openDatabase("../../../mnt/sdcard/demo", "1.0", "demo", 200000);
 console.log("DataBaseObject::::::::::"+db);
 db.transaction(populateDB, errorCB, successCB);

  }

       function populateDB(tx) {

        tx.executeSql('INSERT INTO tbl_DEMO (id, name,  number)VALUES(1,"Firstrow",1)');
         tx.executeSql('INSERT INTO tbl_DEMO (id, name, number) VALUES (2, "Second row",2)');
    }

    // Query the database
   //
    function queryDB(tx) {
        tx.executeSql('SELECT * FROM tbl_DEMO', [], querySuccess, errorCB);
   }

       // Query the success callback
        //
       function querySuccess(tx, results) {
       var len = results.rows.length;
         console.log("DEMO table: " + len + " rows found.");
       for (var i=0; i<len; i++){
          console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  "  + results.rows.item(i).data);
        }
       }

     // Transaction error callback
      //
       function errorCB(err) {
        console.log("Error processing SQL: "+err.code);
        }

       // Transaction success callback
       //
      function successCB() {
      var db = window.openDatabase("../../../mnt/sdcard/demo", "1.0", "Cordova Demo",      200000);
     db.transaction(queryDB, errorCB);
   }

1 个答案:

答案 0 :(得分:0)

我正在调查你的同样的事情。 Is it possible to load/create database in sdcard using phoneGap in Android?

他们在这里说要用     &#34; ../../../../&# 34; 4上层,而不是3层。

祝你好运(对我而言......)..!