Database to csv file
Asked By: Hariprasauth Ramamoorthy
Originally Asked On: 2014-01-02 07:18:41
Asked Via: stackoverflow
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?
He received 1 answers
without selecting any answers.
If the selected answer did not help you out, the other answers might!
All Answers For: Database to csv file
Durgaprasad Budhwani’s answer to
Database to csv file
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('rn'); 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; }); }); });
Of course, you should really check out the original question.
The post Database to csv file appeared first on Tech ABC to XYZ.