0

I am trying to create a local DB in chrome (.db file). I am just trying to create a button that could allow the user to just migrate or create an excel sheet with the data from the database. I am trying to do this in javascript.

Here is what I have tried:

1) I know of ActiveX object that would work only in IE. However, the browser doesnt support th Database operations. So, ActiveX is ruled out.

2) I could use browsers like chrome, however, i am not sure if I can create a csv file through javascript easily. I dont want to export the tables from the HTML. I want a complete control of the csv file just like ActiveX.

Any hints, please?

  • http://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction – adeneo Jan 02 '14 at 07:20
  • the fiddle is just blank.. Am I missing something? – Hariprasauth Ramamoorthy Jan 02 '14 at 07:22
  • the [fiddle](http://jsfiddle.net/nkm2b/2/) creates a CSV file from the given data in javascript and triggers a download of that CSV file, there is no output other than that ? – adeneo Jan 02 '14 at 07:24
  • it doesnt create, unfortunately. I am trying this in chrome. I even tried to do this with a new button. – Hariprasauth Ramamoorthy Jan 02 '14 at 07:25
  • Works just fine for me – adeneo Jan 02 '14 at 07:27
  • Hi Hariprasauth, Please let me know if you want to create local db (In my case, it is in IndexedDB), all tables data to csv ?? – Durgaprasad Budhwani Jan 02 '14 at 07:27
  • CSV is just a simple text format, I don't really see why there would a problem in parsing arbitrary data from a database into CSV format ? – adeneo Jan 02 '14 at 07:29
  • Hi @DurgaprasadBudhwani: precisely what I wanted.. I have a local db which i create through js and now, upon a click of a button, i want it to be exported to an excel sheet/csv file.. Feasible? – Hariprasauth Ramamoorthy Jan 02 '14 at 07:31
  • Please refer my code and let me know if it is working. You might need to make some small changes like change in your table name, columns name etc – Durgaprasad Budhwani Jan 02 '14 at 07:32
  • @adeneo: Even your code works.. But, only in firefox.. Is it feasible to place the csv in some folder path instead of just download? – Hariprasauth Ramamoorthy Jan 02 '14 at 09:36
  • The code works in all browsers except IE, as explained in that answer, so if it's not working in chrome you're doing something wrong. You don't have access to the users computer so you can't just place files wherever you choose, the user has to accept the download and place the file whereever he may choose. If you're trying to store this on the serverside, using a serverside language seems easier. – adeneo Jan 02 '14 at 10:15
  • got it.. The issue was with my chrome browser.. It downloads now.. And please can you show me a code to save it as an xls than csv? – Hariprasauth Ramamoorthy Jan 02 '14 at 10:31

1 Answers1

0

If you want to export any specific table in IndexedDB, then please refer our code below (this is the example how I have exported db data to csv file)

var transactionsRecorder = {};
    transactionsRecorder.webdb = {};
    transactionsRecorder.webdb.db = null;

    transactionsRecorder.webdb.open = function () {
        var dbSize = 5 * 1024 * 1024; // 5MB
        transactionsRecorder.webdb.db = openDatabase("TransactionsRecorder", "1.0", "Transactions Recorder", dbSize);
    }

$(document).ready(function(){
    init();
});

function init() {
    transactionsRecorder.webdb.open();
    transactionsRecorder.webdb.createTable();
}

//Create Table Function
transactionsRecorder.webdb.createTable = function() {
    var db = transactionsRecorder.webdb.db;

    db.transaction(function(tx) {
        tx.executeSql("CREATE TABLE IF NOT EXISTS accounts (accNo INTEGER PRIMARY KEY ASC, accType INTEGER, accAlias TEXT, accAddedOn DATETIME, custId INTEGER, roi DECIMAL, principal DECIMAL, interest DECIMAL, accStatusId INTEGER, lastUpdatedInt DATETIME, lastUpdatedCust DATETIME)", []);
    });

    //Hardcode Insert Queries End

}


 $("#exportAccount").click(function(){
            var db = transactionsRecorder.webdb.db;
            db.readTransaction(function(tx){
                tx.executeSql('SELECT accNo, accAlias, accAddedOn, custId, roi, principal, interest, lastUpdatedInt FROM accounts', [], function(tx, results){
                    var quotechar = '"';
                    var sepchar = ',';
                    var row, rowarray, csvstring;
                    var csvs = [];
                    var fieldnames = ['accNo','accAlias','accAddedOn', 'custId', 'roi', 'principal', 'interest', 'lastUpdatedInt'];
                    // this is the header row
                    csvs.push(fieldnames.join(sepchar));
                    for (var i=0; i<results.rows.length; i++) {
                        row = results.rows.item(i);
                        // you need to make sure you have an explicit order for the csv
                        // row is an object with unordered keys!
                        rowarray = [];
                        for (var j=0;j<fieldnames.length;j++) {
                            rowarray.push(row[fieldnames[j]]);
                        }
                        csvs.push(rowarray.join(sepchar));
                    }
                    csvstring = csvs.join('\r\n');
                    var blob = new Blob([csvstring], {type: "text/plain;charset=utf-8"});
                    var csvDate = new Date();
                    var csvDateStr = csvDate.getDate() + "_" + (csvDate.getMonth() + 1) + "_" + csvDate.getFullYear() +  "_" + csvDate.getTime() ;
                    saveAs(blob, "account_"+ csvDateStr +".csv");
                    // csvstring should now contain a multirow csv string;
                });
            });
        });