我一直在尝试使用适用于 Android 的 phonegap 填充 SQLite DB。我正在关注这篇文章。我已经能够遵循前两个步骤,即使用这个插件。我的问题是:1)当我能够通过第一步填充数据库时,我为什么要遵循第二步和第三步?2)在第三步之后,我陷入了两难境地,因为我在数据>数据> [我的应用程序名称]> app_database中找到了任何名为“0000000000000001.db”的文件。我的 Database.db 位于数据 > 数据 > [我的应用程序名称] > 数据库中。
我的 index.html
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Minimal AppLaud App</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<script type="text/javascript" charset="utf-8" src="SQLitePlugin.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
db.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table
(id integer primary key, data text, data_num integer)');
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)",
["test", 100], function(tx, res) {
console.log("insertId: " + res.insertId + " -- probably 1");
alert("insertId: " + res.insertId + " -- should be valid");
db.transaction(function(tx) {
tx.executeSql("SELECT data_num from test_table;", [], function(tx, res) {
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
alert("res.rows.length: " + res.rows.length + " -- should be 1");
alert("res.rows.item(0).data_num: " + res.rows.item(0).data_num + "should be 100");
});
});
}, function(e) {
console.log("ERROR: " + e.message);
});
});
}
</script>
</head>
<body onload="init();" id="stage" class="theme">
<p>Your app goes here.</p>
</body>
</html>
MyPhonegapActivity.java
package org.mobinius.sqltry1;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import java.io.*;
import android.app.Activity;
import android.view.Menu;
public class MyPhoneGapActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
String pName = this.getClass().getPackage().getName();
this.copy("Database.db","/data/data/"+pName+"/databases/");
//this.copy("0000000000000001.db","/data/data/"+pName+"/app_database/file__0/");
}
catch (IOException e)
{
e.printStackTrace();
}
super.loadUrl("file:///android_asset/www/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists())
{
CheckDirectory.mkdir();
}
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();
}
}
正如您在屏幕截图中看到的,没有名为“0000000000000001.db”的数据库文件。请帮助我。