我正在尝试获取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();
}
答案 0 :(得分:4)
由于您创建的表格显示_id
自动递增,您为什么要插入?
尝试这样的事情: -
db.execSQL("insert into movies(title, year, director) values(" + "'" + moviesTitle + "', " + moviesYear + ", '" + moviesDirector + "');");
从_id
移除insert query
。