【问题标题】:table not found when coming back from in app browser link: Phonegap从应用程序浏览器链接返回时找不到表:Phonegap
【发布时间】:2014-11-01 14:03:17
【问题描述】:

我正在为 android 开发一个 phone-gap 应用程序,在 assets 文件夹中保存带有 .db 扩展名的数据库,在应用程序的 java 文件中我确实将数据库复制到我的应用程序数据库(代码如下),现在假设我有两个按钮(一种用于打开外部链接,另一种用于打开数据库并从数据库中获取数据)。在启动应用程序时,当我单击从 db 按钮获取数据时,它成功完成并执行查询,现在当我单击按钮以从我的应用程序打开 inapp 浏览器链接时,它在 inappbrowser 中打开链接,现在我单击返回按钮,我们现在是我们的应用程序页面,如果我单击从数据库中获取数据的按钮,它的发送错误“SqliteDatabaseCpp(28065): sqlite returned: error code = 1, msg = no such table: tblProduct "

所以请任何人都可以建议我哪里错了,我的复制数据库和执行查询的代码如下:

将数据复制到数据库的 Java 代码:

public void onCreate(Bundle savedInstanceState) {
    try {
    this.copy("Databases.db", "/data/data/" + pName + "/app_database/");
    this.copy("0000000000000001.db", "/data/data/" + pName
            + "/app_database/file__0/");
    super.onCreate(savedInstanceState);

    super.loadUrl(
            "file:///android_asset/www/index.html",
            2000);
    super.appView.setVerticalScrollBarEnabled(true);
    super.appView.setHorizontalScrollBarEnabled(false);
    super.appView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

} catch (IOException e) {
    e.printStackTrace();
}
}

 // Copy Paste this function in the class where you used above part
void copy(String file, String folder) throws IOException {

Log.d("GRT", "We are in the copy of db");
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists()) {
    Log.d("GRT", "Creating copy of db db");
    CheckDirectory.mkdir();
} else {
    Log.d("GRT", "Databse already exists");
}

InputStream in = getApplicationContext().getAssets().open(file);
OutputStream out = new FileOutputStream(folder + file);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
    out.write(buf, 0, len);
in.close();
out.close();

}

HTML按钮(一个用于打开inappbrowser,另一个用于查询数据库):

 <a data-role="button" onclick="openInapp()">open inapp browser</a>

 <a  data-role="button" onclick="queryRecommded()">View Recommended Product</a>

对应的.js文件代码为:

function onDeviceReady()
 {

   var db = window.openDatabase("Database", "1.0", "GRTDB", 3000000);

   db.transaction(queryRecommondedProduct, errorCB2, successCB);


  }

  function queryRecommondedProduct(tx){
    console.log("tx"+tx)
    try{
        //alert("SQL gonna run now");
       var progressHud =window.plugins.waitingDialog;
       progressHud.show("Loading...Please wait");
       tx.executeSql("Select aa.ProductID,aa.ProductName,aa.Price,aa.Description,aa.Specification,aa.VideoLink,ab.GalleryID,ab.Location,ab.ImageName,ab.LocalFolder,aa.Buy_Now from tblProduct aa inner join tblGallery ab on aa.ProductId=ab.ProductId where aa.ProductId=(select RecomProdId  from tblQuery where AnswerQ1='"+"townhouse"+"' AND  AnswerQ2='"+"small"+"' AND AnswerQ3='"+"several"+"' AND AnswerQ4='"+"medium duty"+"' AND AnswerQ5='"+"monthly"+"' AND IsDeleted='0' ORDER BY ProductAnswerId ASC LIMIT 1  ) and aa.IsDeleted ='0' and ab.IsDeleted ='0' and ab.IsDownLoad ='1'", [],function(tx,result){queryRecommdedSuccess(tx,result,"isReccom")}, errorCB4);   

    }
    catch(e){alert("test"+e);}
}
 function queryRecommdedSuccess(tx,result,rflag){

    console.log("Step2"+result.rows.length)
  // alert("Step2 - "+result.rows.length);
    if(result.rows.length>0){


    }
    }

function errorCB2(err){
    alert("Error2 is::--- "+err.code);
}


function successCB(){
}
 function **queryRecommded**()
 {

   document.addEventListener("deviceready", onDeviceReady, false);
 }

 function openInapp(){

  iabRef = window.open('http://apache.org', '_blank', 'location=yes');
  iabRef.addEventListener('loadstart', iabLoadStart);
  iabRef.addEventListener('loadstop', iabLoadStop);
  iabRef.removeEventListener('loaderror', iabLoadError);
  iabRef.addEventListener('exit', iabClose);
 }

【问题讨论】:

    标签: android cordova


    【解决方案1】:

    target 值处使用_system 解决了我的问题。

    window.open(url, target, options);

    • url: 要加载的 URL(字符串)。如果您的 URL 中有 Unicode 字符,请在此调用 encodeURI()。
    • target: 加载 URL 的目标(字符串)(可选,默认值:“_self”)
    • options: InAppBrowser 的选项(字符串)(可选,默认值:“location=yes”)

    target 值:

    _self - opens in the Cordova WebView if url is in the white-list, else it opens in the InAppBrowser
    _blank - always open in the InAppBrowser
    _system - always open in the system web browser
    

    【讨论】:

      【解决方案2】:

      由于我需要使用target= "_blank" 创建inAppBrowser 并使用数据库连接返回,我决定深入调查并找到适合我的“修复”。

      在源文件中(在我的例子中是 Android (InAppBrowser.java))我只是评论那些行是这样的结果:

       
      if (enableDatabase) {
          //String databasePath = cordova.getActivity().getApplicationContext().getDir("inAppBrowserDB", Context.MODE_PRIVATE).getPath();
          //settings.setDatabasePath(databasePath);
            settings.setDatabaseEnabled(true);
                      }
      
      

      【讨论】:

      • 我刚刚遇到了同样的问题,您的修复工作有效 - 非常感谢您发布它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 2016-07-16
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2018-12-17
      • 2020-10-10
      相关资源
      最近更新 更多