0

I'm reading this CSV:

A;San Antonio;Tercer Barrio
B;Camino de la Miranda;Campus
C;San Lázaro;Cementerio
E;Cristo;H. Río Carrión
H;H. San Telmo;H. Río Carrión
M;Plaza de León;Monte el Viejo
P;Allende el Río;Campus

and I want to populate a SQLite database in Android using this method:

private void insertRoutes(SQLiteDatabase db) {
  try {
     BufferedReader reader = new BufferedReader(
             new InputStreamReader(context.getAssets().open(SQL_FILE_ROUTES)));

     String query =
             "insert into routes (name, rfrom, rto) values (?, ?, ?)";

     db.beginTransaction();
     SQLiteStatement stmt = db.compileStatement(query);
     String line;

     while ((line = reader.readLine()) != null) {
        String[] dic = line.split(";");
        stmt.bindString(1, dic[0]);
        stmt.bindString(2, dic[1]);
        stmt.bindString(3, dic[2]);
        stmt.execute();
        stmt.clearBindings();
     }
     db.execSQL(query);
     db.setTransactionSuccessful();

  } catch (IOException e) {
     e.printStackTrace();
  } finally {
     db.endTransaction();
  }
}

It "works" and my database get populated, but with and extra row. This is the result when I check the sqlite database:

sqlite> select * from routes;
1|A|San Antonio|Tercer Barrio
2|B|Camino de la Miranda|Campus
3|C|San Lázaro|Cementerio
4|E|Cristo|H. Río Carrión
5|H|H. San Telmo|H. Río Carrión
6|M|Plaza de León|Monte el Viejo
7|P|Allende el Río|Campus
8|||

There are 8 rows, but the CSV file has just 7 lines. What I'm doing wrong? Thanks.

EDIT

I have checked the file. It has 7 lines:

wc default_android_routes.csv 
7  25 198 default_android_routes.csv

EDIT

Like @flopo has said, db.execSQL(query); was the guilty. Just remove it.

5
  • Are you certain that the CSV file does not have a blank line at the bottom?
    – Yjay
    Jan 15, 2014 at 20:32
  • Yes, I have checked it (that was the first thing that came to mind).
    – Marcos
    Jan 15, 2014 at 20:34
  • 1
    Have you put a system out in your your loop so you can see exactly what it is parsing and how many times the loop runs on the java side? Also is it possible your table already had an entry in it?
    – ug_
    Jan 15, 2014 at 20:39
  • have you tried FileReader with while ((line = fileReader.hasNextLine()) != null) ? it may be a simple linebreak that causes the extra line.
    – GameDroids
    Jan 15, 2014 at 20:40
  • Actually I'm thinking it's an Android related problem. I have put a Log.v() inside the loop and 7 lines appears.
    – Marcos
    Jan 15, 2014 at 20:41

2 Answers 2

1

Not pretty sure, but I think db.execSQL(query) is the one that is causing your extra row.

In the while loop you execute the statement for each row. Outside the loop, you don't need to execute again the query.

As I said, I haven't used SQLiteDatabase, but I think that's the problem you have.

0
1

CSV may have blank line, check in text pad. Try selecting the whole text file and see whether the next line is also getting selected.

You can do this to avoid empty line.

while ((line = reader.readLine()) != null) {
        if (line.trim().length() == 0) { // this will check empty line
             break;
        }
        String[] dic = line.split(";");
        stmt.bindString(1, dic[0]);
        stmt.bindString(2, dic[1]);
        stmt.bindString(3, dic[2]);
        stmt.execute();
        stmt.clearBindings();
     }
6
  • I also tried that. But the same occurs. Maybe it's an Android related problem? I don't know.
    – Marcos
    Jan 15, 2014 at 20:41
  • Did you try printing the line, does it print empty line? If length is zero, it can't proceed.
    – Vaandu
    Jan 15, 2014 at 20:43
  • Nope, it prints 7 lines. That's because I'm thinking my method is correct.
    – Marcos
    Jan 15, 2014 at 20:43
  • Did you try reading the csv file first into a List and after that inserting it into the database? That may be overhead, but at least you could either check the Strings in the list or just skip the last line
    – GameDroids
    Jan 15, 2014 at 20:45
  • So stmt.execute(); is getting executed only 7 times. what does this do then "db.execSQL(query);" Is this adding empty row?
    – Vaandu
    Jan 15, 2014 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.