HTML5 database storage (SQL lite) - few questions
Asked Answered
J

5

26

Hy there,

I can't find enough beginner resources on the web about HTML5 database storage usage examples (CRUD)

I'm opening(creating) my DB like this:

var db;

$(document).ready(function() 
{

    try
    {
      if (!window.openDatabase) {
            alert('Not Supported -> Please try with a WebKit Browser');
      } else {
          var shortName = 'mydatab';
          var version = '1.0';
          var displayName = 'User Settings Database';
          var maxSize = 3072*1024; //  = 3MB            in bytes 65536
          db = openDatabase(shortName, version, displayName, maxSize);      
          }
    } 
    catch(e) 
    {
      if (e == 2) {

          alert("Invalid database version.");
      } else {
          alert("Unknown error "+e+".");
      }return;
    }
});

QUESTION 1: How many databases can i create and use on one domain? QUESTION 2. How to delete (drop) a database. -> i have not figured this out yet.

To create sql queries you use transaction:

function nullDataHandler(transaction, results) { }
function createTables(db)
{
  db.transaction(function (transaction)
  {
    //first query causes the transaction to (intentionally) fail if the table exists.
    transaction.executeSql('CREATE TABLE people(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL DEFAULT "John Doe", shirt TEXT NOT NULL DEFAULT "Purple");', [], nullDataHandler, errorHandler);
  });
}

QUESTION 3: How so is the above transaciton failed if a table exists? Is the nullDataHandler involved to do this? Where on the web is there documentation explaining the executeSql API? Arguments?

thx

Jolynjolynn answered 22/4, 2010 at 10:21 Comment(1)
Hey I have written out this basic user guide which has a few worked out examples.... Hope they can be of some help.... mnesia.wikispaces.com/HTML5+and+webstorage As of now I think no one is really paying a lot of attention to WEB SQL based storage.... And that is understandably so... The key-pair based local-storage system works very well for most web apps.... Reminds me of how redis and memcache have taken off as compared to RDBMS... Especially for apps that dont really need that kind of structure in there data...Masefield
G
32

The spec you're looking for is Web SQL Database. A quick reading suggests:

  1. There is no limit, although once your databases increase beyond a certain size (5MB seems to be the default), the browser will prompt the user to allow for more space.
  2. There is no way, in the current spec, to delete databases.
  3. The executeSql() function takes an optional error callback argument.

HTML5 Doctor also has a good introduction.

Going forward, though, I'd recommend looking at Indexed DB. Web SQL has essentially been abandoned since there is no standard for SQL / SQLite. Even Microsoft has endorsed Indexed DB. See Consensus emerges for key Web app standard.

Genous answered 22/4, 2010 at 11:43 Comment(3)
Thanks Jeff. I'm almost done reading the Consesus - it's very informative, as the rest of what you said. As you see, point & answer added. But before i leave..what should i use now to store user data? It's not many data..but things i need to remember for a user..like some website adresses etc.. so the next time he logins the list will be in a side-list. Now, hhave to mention it's an iphone web application -> that's why i use WEB DB. (but i don't like it).Jolynjolynn
It's great to have some html5 features in your pocket. I like the: sessionStorage and localStorage mechanisms...i stopped using query_string and pass needed data with those two. I'm just not so sure for how long, what is the persistance of localStorage and there are some other reasons beyond these why i think WEB DB would do good.Jolynjolynn
Data stored with localStorage should persist until the user decides to clear it (Settings > Safari > Databases). I suggest taking a look at Apple's documentation: developer.apple.com/safari/library/documentation/iPhone/…Genous
T
2
CREATE TABLE IF NOT EXISTS table_name

will create a table table_name only if if does not exist.

Triatomic answered 15/2, 2012 at 2:5 Comment(0)
R
1

I found the following WebSQL tutorials helpful for basic CRUD operations, as they contained examples, and explained what the code was doing:

And the following links for SequelSphere (an HTML5 JavaScript SQL Relational Database Alternative to WebSQL that works in all browsers, storing data in LocalStorage and IndexedDB):

Restaurateur answered 9/2, 2013 at 15:52 Comment(2)
The Web SQL database is deprecated, isn't it?Gosling
@Gosling It's not deprecated. W3 will not improve documentation of websql once all vendors implemented the same sql engine,SQLite, but it is still a documented functionality and will continue to be part of standard definitionMauney
O
1

Using PersistenceJS there is a persistence.reset API which will wipe the database clean. PersistenceJS Site

For developing / testing purposes, you can view content and delete webSQL, IndexedDB, cookies, etc by searching for your domain name at this URL in Chrome:

chrome://settings/cookies

There, you can delete all the storage for a domain or just certain local storage entities. Yes, the URL implies just 'cookies', but the interface at this URL includes all types of offline storage.

It would be great I think if the Chrome developer tools interface had the ability to right-click and delete a data storage entity in the Resources tab along with inspecting the content. But for now, all I know of is the settings/cookies URL.

Occasion answered 25/3, 2013 at 14:0 Comment(0)
H
1

It is supported on iOS safari,chrome and some latest version of opera....it's not yet adopted by IE and Firefox that's it......what more one can ask than local db on browser which has relational db system...so u can query it easily and handle complex data....which is very tougher in key vale based systems..

I remember reading it even supports upto one gb.i am not sure....

Note:

1)I'd like to mention one point there is a IDE called Dashcode which let's u build web apps that looks like iOS native.even there also web SQL is used.

2)actually web SQL is a implementation of SQLite on browsers.

3)SQLite is most prefered in both iOS and android as db for native code..

The drawbacks of SQLite:

The Lack of concurrency support but which is not a problem in browser as it's gonna be used by single user at a time..this is a case also in mobile.

Conclusions:

Web Sql is abandoned by w3 that's a sad thing so we've to explore other options.

Howdah answered 21/4, 2013 at 22:38 Comment(3)
More specifically, support for the spec has been abandoned, however, many platforms still support the APIs. The question is - since further development and support of the spec have stopped, how long will the APIs be supported on various platforms. That is why @Jeffery To suggested migrating to Indexed DB.Dutch
Yes I agree with you all i didn't read the documentation clearly..Thanks for pointing out...Howdah
Now I have changed the contentHowdah

© 2022 - 2024 — McMap. All rights reserved.