Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export (dump) WebSQL data

I'm working on a Chrome Extension that uses WebSQL to store historical data. Being WebSQL, the DB is stored on the client.

I'd like to add an option to export/import such data so that the user can share/use this data with other users, or with other PCs.

These are my first steps on a client-only database, so I wonder how to do this. I was thinking to convert the DB to a huge json string that the user can copy/paste but doesn't look very user-friendly.

Is there any better solution?

like image 240
Omiod Avatar asked Oct 16 '10 09:10

Omiod


People also ask

Is Websql deprecated?

Web SQL was deprecated and removed for third-party contexts in Chromium 97. [📍 We are here.] Web SQL access in insecure contexts is deprecated as of Chromium 105 at which time a warning message will be shown in the Chrome DevTools Issue panel.

What is Websql in Chrome?

Web SQL Database is a web page used for storing or managing the data in the database. The API is supported by Google Chrome, Opera and Android browsers. The Web SQL API is not a part of the HTML5 specification, but is a separate specification. It addresses a set of APIs to manipulate the client-side database.

How can we use Websql?

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.


1 Answers

I got a single table dump solution working on a HTML5 database client I wrote a few days ago.

Check out http://html5db.desalasworks.com/script.js and scroll down to SqlClient.exportTable, this has an example that needs to be expanded to cover the whole database.

The steps are:

Step 1: Create the schema:

SELECT sql FROM sqlite_master

Step 2: Get a list of tables:

SELECT tbl_name from sqlite_master WHERE type = 'table'

Step 3: Loop through each of them and create an INSERT script with the results

transaction.executeSql("SELECT * FROM " + _tbl_name + ";", [], 
    function(transaction, results) {
        if (results.rows) {
            for (var i = 0; i < results.rows.length; i++) {
                var row = results.rows.item(i);
                var _fields = [];
                var _values = [];
                for (col in row) {
                    _fields.push(col);
                    _values.push('"' + row[col] + '"');
                }
                _exportSql += ";\nINSERT INTO " + _tbl_name + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")";
            }
        }
    }
);

Hope this is useful.

UPDATE JAN 2016 - WHOLE DB EXPORT

I've got a JS websqldump library that you can download from github as well.

This one will export the whole database. Check out the code on:

https://github.com/sdesalas/websqldump

Usage as follows

websqldump.export({
  database: 'NorthwindLite',
  success: function(sql) {alert(sql);}
});
like image 101
Steven de Salas Avatar answered Sep 20 '22 21:09

Steven de Salas