开发者

load webstorage from native sqlite

I have a native sqlite database and i want to load/read it through HTML5 webstorage. But I am unable to do that. I already tried it by placing the file in the same folder and running the below script.

<html>
<head>
<title> Search </title>
<script>
var db = null;
function startapp(){
db = window.openDatabase("artistsWcities.sqlite", '1.0', 'Test', 19 * 1024 * 1024);
开发者_Go百科if (db) {
alert("database loaded");
}
}
</script>
</head>
<body onload="startapp()">
</body>
</html>

But it created new file instead of reading new one. How to do this?


In W3C specification openDatabase constructor apply first argument only like browser database name, not any filename. Specification quote:

The user agent must act as if the database was hosted in an otherwise completely empty environment with no resources. For example, attempts to read from or write to the file system will fail.

You can get sqlite file from local resources only in browser extensions or components, for example in Firefox.

You get your file artistsWcities.sqlite by request from server, for example, AJAX and in success callback past it to browser local database. More info in this question.

But if your code example is from extension for browser, read closely the documentation. If Firefox extension you can load local database via:

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

let file = FileUtils.getFile("ProfD", ["my_db_file_name.sqlite"]);
let mDBConn = Services.storage.openDatabase(file); // Will also create the file if it does not exist
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜