适用于Android的Phonegap填充Sqlite数据库

时间:2013-09-13 07:39:23

标签: android cordova android-sqlite phonegap-plugins

我一直在尝试使用适用于Android的phonegap填充SQLite数据库。我正在关注此post。我能够按照前两个步骤使用this plugin。 我的问题是: 1)当我能够通过第一步填充数据库时,我为什么要遵循第二步和第三步? 2)在第三步之后,当我在数据>中找到任何名为“0000000000000001.db”的文件时,我陷入了两难境地。数据> [我的应用程序名称]> app_database。而我的Database.db就在 数据>数据> [我的应用程序名称]>数据库。

enter image description here

我的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”的db文件。请帮帮我。

0 个答案:

没有答案