I'm writing a demo application to see if it is possible to share a database between native android and a web view. For this i added a webview and a button to my activity. On click of button i create a database in /data/data/com.demo/databases/testdb. I can see this database on sqlite3 prompt. Then i wrote a html page which has following script tag in head section
<script type="text/javascript">
if (window.openDatabase){
db = window.openDatabase("testdb", "1", "test database", 1024*1024);
alert("Database " +开发者_JAVA百科 db);
if(db){
db.transaction(function(tx)
{
tx.executeSql('SELECT * FROM KeyValues',
[],
function (tx, rs) { displayMyResult(rs); },
function (tx, err) { displayMyError(err); } );
});
}
}else{
alert("Database is not supported");
}
function displayMyResult(rs){
alert("In display method");
for(var i=0; i < rs.rows.length; i++) {
r = rs.rows.item(i);
alert('id = ' + r['id'] + ', Key: ' + r['key_name'] + ', Value: ' + r['key_value']);
}
}
function displayMyError(err){
alert(err.message);
}
</script>
Issue is i'm always getting the database object db as null. May be this is not even possible. or am i missing something.
Any help is appreciated.
Edit : Bottom line : is it possible to access the database created by native android API in webview?
Have you tried reading this post How do I call window.openDatabase from a webView hosted in an android application?
You need to do
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
...
settings.setDatabaseEnabled(true);
settings.setDatabasePath("/data/data/your.package.name/database_name");
精彩评论