Damon
Damon

Reputation: 10809

How can I import data from a CSV file into Adobe Air app?

I am going to be writing an app in flash builder and I want the ability to import CSV data into an SQLite database in Adobe Air/flashbuilder.

I have never done anything like this and am not totally sure where to start. I will want to do some manipulation of the data before I reinsert it. So really I need to know what steps (classes and functions) I need to use to parse the data from a CSV file into an array that can be manipulated in actionscript?

Upvotes: 3

Views: 3593

Answers (2)

Chris Dolan
Chris Dolan

Reputation: 8963

[Update: my answer assumes HTML/Javascript AIR, as opposed to Flash/Actionscript. There may be better answers on the AS3 side...]

You can always go the low-level approach of reading it via a FileStream and writing a CSV parser yourself. I doubt you'll find a pre-existing library to input an arbitrary CSV into SQLite, but maybe I'm wrong (I haven't looked).

The basics you want to start with for your FileStream reader would be like this example code that reads a line at a time of asynchronous input. The example is reading from the STDOUT of a separate process, but should apply to a FileStream straightforwardly.

this.process.addEventListener(air.ProgressEvent.STANDARD_OUTPUT_DATA, function(event) {
   // call into _stdoutHandler...
});

Foo.prototype._stdoutHandler = function() {
   var nBytes = this.process.standardOutput.bytesAvailable;
   var msg = this.process.standardOutput.readUTFBytes(nBytes);
   // The native process might send us partial lines, due to async IO.
   // Reconstruct the whole lines.
   this._sofar += msg;
   while (true) {
      var idx = this._sofar.indexOf("\n");
      if (idx < 0)
         break;
      if (idx > 0) { // skips blank lines
         var line = this._sofar.substring(0, idx);
         this._foundLine(line);
      }
      if (this._sofar.length > idx + 1) {
         this._sofar = this._sofar.substring(idx+1);
      } else {
         this._sofar = "";
      }
   }
   var lines = this._sofar.split(/\n/);
   if (lines.length > 1) {
      air.trace("programming error: we should have already handled all newlines");
   }
   this._sofar = lines[lines.length - 1];
};

Foo.prototype._foundLine = function() {
   // process a single line of input here...
};

Upvotes: 1

Daniel
Daniel

Reputation: 35684

I use casalib for a lot of stuff, including loading. You can use the standard as3 stuff, but this makes it a lot easier. Look into CasaLoader.

You can then use some string splitting to extract the rows/columns

the sqlite stuff is surprisingly easy. Have a look at this link: http://ntt.cc/2008/07/08/sqlite-example-for-adobe-air-working-with-local-sql-databases-with-source-code.html

you can ignore the mxml stuff

Upvotes: 1

Related Questions