Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSQL syntax on IndexedDb

I have an application that use WebSQL. I want to support IndexedDB too (For Browsers without WebSql support). Is there any libary that use sql-syntax but works with indexedDB / LocalStorage in background?

I don't want to change all my querys and functions.

All libraries i found uses IndexedDb syntax and support WebSql. (Not what I need).

Thanks :)

like image 981
Cracker0dks Avatar asked Feb 13 '14 14:02

Cracker0dks


People also ask

What is IndexedDB and Web SQL?

WebSQL Database is a relational database access system, whereas IndexedDB is an indexed table system. Don't start working with IndexedDB, relying on your assumptions from other types of databases. Instead, you should read the docs carefully.

How can we use Web SQL?

Web SQL is a web page API for storing or managing the data in databases that can be queried using a variant of SQL like creating databases, opening the transaction, creating tables, inserting values to tables, deleting values, and reading data.

Is IndexedDB deprecated?

The W3C has announced that the Web SQL database is a deprecated local storage specification so web developer should not use this technology any more. indexeddb is an alternative for web SQL data base and more effective than older technologies.


1 Answers

You can try Alasql JavaScript SQL database library. It supports all important SQL statements and can work with IndexedDB with SQL syntax as well.

Here is an example:

<script src='alasql.min.js'></script>
<script>
    var cityData = [{city:"Redmond", population:57530},
        {city:"Atlanta",population:447841},
        {city:"San Francisco", population:837442}];

    // Create IndexdDB database and fill it with data from array
    alasql('CREATE INDEXEDDB DATABASE IF NOT EXISTS geo;\
        ATTACH INDEXEDDB DATABASE geo; \
        USE geo; \
        DROP TABLE IF EXISTS cities; \
        CREATE TABLE cities; \
        SELECT * INTO cities FROM ?', [cityData], function(){

        // Select data from IndexedDB
        alasql('SELECT COLUMN * FROM cities WHERE population > 100000 ORDER BY city DESC',
           [],function(res){
                document.write('Big cities: ', res.join(','));
        });
    });
</script>

You can play with this example in jsFiddle

like image 166
agershun Avatar answered Oct 13 '22 01:10

agershun