我正在为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 按钮(一个用于打开应用浏览器,另一个用于查询数据库):
<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);
}