Domanda

I've got a rather large dataset from a client that is in csv and I need it to be converted to sqlite in Android. So I have a method I wrote to do it -

db.beginTransaction();    
while ((line = bufRdr.readLine()) != null) {
        try {
            insertValues = line.split(",");

            if (flag_is_header) {
                row = this.insertRow(insertValues[0], insertValues[1], insertValues[2], insertValues[3], insertValues[4]);

            } else {
                flag_is_header = true; //so it skips the first line
            }
        } catch (Exception e) {
            Log.e(TAG,"row# "+row+" error");
        }

    }
    db.setTransactionSuccessful();
    db.endTransaction();

and it works great. 9k rows inserted in under 5 seconds.

The problem is that the some of the data has line breaks within the cells. ie this -

Steps needed
1 .....
2 .....
3 .....

can be the value of one cell, so when my loop hits the line breaks (within the cell) it thinks that there is a new row to be inserted and it breaks the code.

Is there another way to import the data rather than using line breaks, or should I be doing something else entirely?

Thank you for your help

ps. i cannot pre-populate the database and ship it for logistic reasons.

È stato utile?

Soluzione

While exporting, you could replace line breaks in cells with (let's say) tabs (or a combiantion of strange chars, like "§#§").
Then, when reimporting, replace the tabs with line breaks again

[EDIT]

Oh, it's from Excel... nice. It's even better, then!!
You can export rows having the cells surronded by "".
By doing so, you know what belongs to the cell and which is the real row terminator.
While importing, just remove the extra quotes.

[EDIT]

In excel, when you export as csv, you can choose to save the cells beding "quoted", so ALL the cells will be quoted.

Like:

"CellA1", "CellA2", "This is a cell with spaces", "CellA4"
"CellB1", "CellB2", "CellB3", "This is a cell
with
carriage returns"

It's automated while exporting if you choose to do so

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top