2

This is in reference to my question - Attach content of HTML5 WebSQL databse to email using javascript

I have an HTML5 app that will run in Chrome browser. I need to export the data stored locally in the Web SQL DB and put it into a csv or txt file.

I need to do this in the browser. Server side code is not an option. How can this be done usng javascript? Thanks

EDIT: I found that one way of achieving this is through Data URI. So I have my result set from select query in the results object. How do I encode it to base 64 format?

       db.transaction(function (tx) {
       tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
       var len = results.rows.length, i;
       msg = "<p>Found rows: " + len + "</p>";
       document.querySelector('#status').innerHTML +=  msg;
       for (i = 0; i < len; i++){
          alert(results.rows.item(i).log );
       }
     }, null);
    });

    //Encode the results object to base64
    /* How can we do this? */

     <a download="example.csv" href="data:application/csv;charset=utf-8, <<base64_encoded_data>>">Test</a>
Community
  • 1
  • 1
user32262
  • 8,660
  • 21
  • 64
  • 77
  • I haven't used WebSQL before so I can't help with that part of things, but if you can get your data into an array, in which each element of the array is an array that represents a row from the database you could use my [CSV library](http://www.uselesscode.org/javascript/csv/) to format it as CSV. Simply include the library and then run your array through `CSV.arrayToCsv();` to receive the array formatted in CSV. – Useless Code Dec 04 '12 at 12:34

1 Answers1

0

I am answering my own question as I figured out a solution.

I created a string from the query result set and converted the string to base64 encoding using the btoa() api.

ocodo
  • 29,401
  • 18
  • 105
  • 117
user32262
  • 8,660
  • 21
  • 64
  • 77