1

我正在尝试获取一个CSV文件并将该数据导入 Android 的SQLite3. 该应用程序运行良好,没有任何SQLite错误LogCat。我不确定为什么没有输入数据。我确保程序实际上execSQL通过故意SQLite在插入命令中导致语法错误来尝试进行插入,并且它出现在LogCat. 所以它确实找到了CSV文件并且确实到达了 insert execSQL。我只是不确定为什么它没有插入。

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();
    }
4

1 回答 1

4

既然你创建表说它_id自动递增的,你为什么要插入它?

尝试这样的事情: -

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

where_idinsert query.

于 2013-03-08T04:19:14.283 回答