Android - SQLite不插入行

时间:2013-03-08 04:13:09

标签: android sqlite

我正在尝试获取CSV文件并将该数据导入Android的SQLite3。该应用运行良好,SQLite中没有任何LogCat错误。我不确定为什么没有输入数据。我确保程序实际上尝试通过在insert命令中故意导致execSQL语法错误来执行插入SQLite,并且它出现在LogCat上。所以它确实找到了CSV文件,它确实找到了插入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();
    }

1 个答案:

答案 0 :(得分:4)

由于您创建的表格显示_id 自动递增,您为什么要插入?

尝试这样的事情: -

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

_id移除insert query