私はAndroid用の電話ギャップアプリケーションを開発しています。アセットフォルダーに.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 ボタン (1 つは appbrowser で開き、もう 1 つはデータベースのクエリ):
<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);
}