1

I'm trying to take a CSV file and importing that data into Android's SQLite3. The app runs fine without any SQLite error in the LogCat. I'm not sure why the data isn't being inputted. I made sure that the program actually tries to do the insert execSQL by deliberately causing a SQLite syntax error in the insert command and it showed up on the LogCat. So it does find the CSV file and it does get to the insert execSQL. I'm just not sure why it isn't inserting.

private static final String CREATE_TABLE_MOVIES = "CREATE TABLE movies (_id integer primary key autoincrement, title text not null, year integer not null, director text not null);";

public DbAdapter(Context ctx){
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
    mContext = ctx;
    this.mDb = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_MOVIES);

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(mContext.getAssets().open("movies.csv")));
        String line;

        while((line = in.readLine()) !=null) {
            String[] RowData = line.split(",");

            moviesID = RowData[0];
            moviesTitle = RowData[1];
            moviesYear = RowData[2];
            moviesDirector = RowData[3];
            db.execSQL("insert into movies(_id, title, year, director) values(" + moviesID + ", '" + moviesTitle + "', " + moviesYear + ", '" + moviesDirector + "');");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
2
  • Caught any exception? Commented Mar 8, 2013 at 4:14
  • No exceptions anywhere that I know of. LogCat and Console is clean. Commented Mar 8, 2013 at 4:15

1 Answer 1

4

Since you create table says that the _id is auto incremented, why do you want to insert that?

Try something like this:-

db.execSQL("insert into movies(title, year, director) values(" + "'" + moviesTitle + "', " + moviesYear + ", '" + moviesDirector + "');");

where _id is removed from the insert query.

Sign up to request clarification or add additional context in comments.

1 Comment

You are one sexy man, you know that? I removed autoincrement. It worked. Can't believe I missed that. But that's what's great about giving your code a fresh eye.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.