HTML5 - 从本地文件加载 Web SQL DB?
让我们在这里使用一个很棒的演示。
假设我作为“管理员”创建了 5 张便签。我的浏览器有一个 SQLite DB,其中包含这 5 个便签及其各自的位置和文本。然后,我将此数据库文件导出到托管该页面的本地服务器。然后,假设另一台计算机上的“用户”加载此页面,并且默认情况下看到我的 5 个便笺;如何使页面从本地文件(例如 /var/www/html/db_files/5-sticky-notes.db)加载 SQLite 数据库,以便最终用户可以与我的便笺进行交互?
这是从最终用户的个人浏览器加载其数据库的代码:
var db;
try {
if (window.openDatabase) {
db = openDatabase("5-sticky-notes", "1.0", "HTML5 Database API example", 200000);
if (!db)
alert("Failed to open the database on disk. This is probably because the version was bad or there is not enough space left in this domain's quota");
} else
alert("Couldn't open the database. Please try with a WebKit nightly with this feature enabled");
} catch(err) {
}
Let's use a great demo as an example here .
Let's say I create 5 sticky notes as an "administrator". My browser has a SQLite DB with these 5 sticky notes and their respective positions and text. I then export this DB file to the local server where the page is hosted. Let's then say that a "user" on another computer loads this page up and, by default, sees my 5 sticky notes; how do I make the page load a SQLite DB from a local file, e.g. /var/www/html/db_files/5-sticky-notes.db, so that end-users can interact with my sticky notes?
This is the code for loading the end-user's database from their personal browser:
var db;
try {
if (window.openDatabase) {
db = openDatabase("5-sticky-notes", "1.0", "HTML5 Database API example", 200000);
if (!db)
alert("Failed to open the database on disk. This is probably because the version was bad or there is not enough space left in this domain's quota");
} else
alert("Couldn't open the database. Please try with a WebKit nightly with this feature enabled");
} catch(err) {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我找到了这个旧问题的答案:
DEMO Here
简短的示例代码(由网站提供):
附加信息
这只适用于支持webDB标准的浏览器(桌面或移动)
I think i found an answer to this old tread:
DEMO Here
Short sample code (provided by the website):
ADDITIONAL INFORMATION
This only works in browsers (either desktop or mobile) that support the webDB standard
浏览器中无法本地执行此操作,但我认为这是可能的。
您将发起一个 Ajax 请求,将数据从本地数据库发送到服务器,然后访问您网站的新用户也会收到一个 Ajax 请求,将数据从服务器拉取到本地数据库中。
非常非常粗糙的伪代码:
但是,如果您要允许其他人查看您的便签,为什么还要费心使用本地 HTML5 数据库呢?只需将它们存储在服务器上?
编辑:我还应该指出,WebSQL 是一个即将消亡的标准,正在逐步淘汰并被 IndexedDB 取代。
There's no way to do this natively in the browser, but it is possible I reckon.
You'd have initiate an Ajax request to send the data from your local database to the server, then a new user visiting your website would also have an Ajax request to pull down the data from the server, into their local database.
Very very rough pseudo code:
However, if you're going to allow other people to look at your sticky notes, why bother with a local HTML5 database at all? Just store them on the server?
Edit: I should also point out that WebSQL is a dieing standard, being phased out to be replaced with IndexedDB.