Creating SQLite Table from Android HTML Assets
I tried to use a HTML in android to create a table in sqlite but it is not working
Sample HTML is here
<!DOCTYPE html>
<html>
<head>
<title>Contact Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
//document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
var db = openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
// Populate the database 开发者_运维问答
//
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Transaction error callback
//
function errorCB(tx, err) {
alert("Error processing SQL: "+err);
}
// Transaction success callback
//
function successCB() {
alert("success!");
}
</script>
</head>
<body onload="onDeviceReady()">
<h1>Example</h1>
<p>Database</p>
</body>
</html>
Android 2.2 I am not able to see any database create in android. Please some suggestion
I think your database created in android's cache directory, that's why to are unable to see it. so use WebView's setting method and make cache enable also give WebView's SQLite database path to your cache directory then use your database.
If I am wrong then please let me know it.
EDIT: look at this,
Step 1: Create a WebView
,
Step 2. Get the webSettings
, and set the Database Path. The path should be the path to your databases, which will be /path/path/your_package_name/
.
appView.setWebChromeClient(new WebClient(this));
WebSettings settings = appView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setDatabaseEnabled(true);
settings.setDatabasePath("/data/data/<database path>/app_database");
Step 3: Create a WebChromeClient, and override the onExceededDatabaseQuota method. This is called when you first open the application because there’s initially no database setup. This will allow you to set the quota in Java. Since this was test code, I just threw an arbitrary value in here.
final class WebClient extends WebChromeClient {
Context mCtx;
WebClient(Context ctx)
{
mCtx = ctx;
}
public void onExceededDatabaseQuota(String url, String databaseIdentifier,
long currentQuota, long estimatedSize,
long totalUsedQuota,WebStorage.QuotaUpdater quotaUpdater)
{
Log.d(LOG_TAG, "We exceeded the quota");
quotaUpdater.updateQuota(100000);
}
}
EDIT: for more details look at here.
Thanks.
On rooted device go to :
- DDMS
- File Explorer
- data ->data
- package name
- app_databases
Here "app_databases" contains "Databases.db" file that contains other databases files .
WebView has the ability to invoke java code through javascript code. How about trying this solution?
精彩评论