简体   繁体   中英

Check if database exists in JavaScript

openDatabase(DB_SHORT_NAME, DB_VERSION, DB_DISPLAY_NAME, DB_MAX_SIZE)

in JavaScript opens database or creates the database if not exists in browser.

Is there any way I can able to check if database exists or not in browser?

JavaScript coined HTML5 is the only way to do this; you can use mobile libraries such as Phonegap or Titanium which will also render a Javascript library - however these are for mobile app development only; there mobile web versions are abysmal so don't consider converting it to mobile web and editing the fluid width values!

Do not forget that not all HTML5 browsers even support the database feature. It is limited. And so is support and it really is about finding out tutorials and trial and error. Those people that do; either blog it, or don't have a blog to post experiences (or so lazy and frustrated blogging about the headache is the last thing!).

http://www.tutorialspoint.com/html5/html5_web_sql.htm

https://stackoverflow.com/questions/8007917/check-if-db-exists-and-dropping-db-in-sqllite-ios

Some of many sites. ( shudders for the angry negative mob )

You could try something like this:

// Uses jQuery
var db="mydb.db";
$.ajax({
    url:db,
    type:'HEAD',
    error: function()
    {
        //file not exists
    },
    success: function()
    {
        //file exists
    }
});

Here is the code for checking, whitout using jquery

    function dbExists(db)
    {
        var http = new XMLHttpRequest();
        http.open('HEAD', db, false);
        http.send();
        return http.status!=404;
    }

var db="mydb.db";
dbExists(db);

Code is not really a live code; nor should I expect it to be - I know it is ugly.. but you get the idea surely. HTML5 local database and storage is poor. What is it you are trying to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM