Error in Firefox when making SQLite calls via Javascript
I have a site that is fully functioning in Chrome/Safari and heavily relied on SQLite to st开发者_如何学Pythonore/access data. However, when testing in Firefox, it errors on the first call, openDatabase(). This is my fairly standard openDB function:
function openDB(){
try {
if (!window.openDatabase) {
alert('not supported');
} else {
var shortName = 'tales';
var version = '1.0';
var displayName = 'Tall Tales Database';
var maxSize = 65536; // in bytes
db = openDatabase(shortName, version, displayName, maxSize);
// You should have a database instance in db.
}
} catch(e) {
// Error handling code goes here.
if (e == 2) {
// Version number mismatch.
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
}
Like I said - openDatabase is undefined when I alert it, and the Unknown error that prints is "unsupported". I assume SQLite is actually supported in Firefox, am I doing something wrong or does it require browser-specific code?
Thank you! Claudia
Firefox doesn't have that feature. Mozilla doesn't think SQLite is appropriate for the web, so in Firefox 4 they will be instead opting for and implementing the IndexedDB spec. draft by the W3C (incl. proposals by Mozilla). Here's a nice blog post detailing the differences: http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/
So yes, you will have to use client specific code in order to support FF4 - at least until IndexedDB is implemented in the other major browsers. For anything before FF4, there is no support for any client database (not counting localStorage, etc.).
It should be:
if (typeof(window.openDatabase)=='undefined') {
alert(...)
function openDB(){
try {
if (!!window.openDatabase) {
var shortName = 'tales';
var version = '1.0';
var displayName = 'Tall Tales Database';
var maxSize = 65536; // in bytes
db = openDatabase(shortName, version, displayName, maxSize);
// You should have a database instance in db.
} else {
alert('not supported');
}
} catch(e) {
// Error handling code goes here.
if (e == 2) {
// Version number mismatch.
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
That should work.
精彩评论