나는이 CSV 읽고 있어요 :읽기 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
을 나는이 방법을 사용하여 안드로이드에 SQLite 데이터베이스를 채우려 :
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();
}
}
그것은 "작품" 내 데이터베이스가 채워지지만 추가 행이 있습니다. 이것은 sqlite 데이터베이스를 확인할 때의 결과입니다.
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|||
CSV 파일에는 8 행만 있지만, 7 행만 있습니다. 내가 뭘 잘못하고있어? 감사합니다. .
내가 파일을 검사 한
편집 할 수 있습니다. 그것은 7 개 라인이 있습니다 @flopo처럼
가 말했다
wc default_android_routes.csv
7 25 198 default_android_routes.csv
편집을 db.execSQL(query);
는 유죄이었다. 그냥 제거하십시오.
CSV 파일의 맨 아래에 빈 줄이없는 것이 확실합니까? – Yjay
예, 확인해 보았습니다 (마음에 가장 먼저 들었습니다). –
시스템을 루프에 넣었습니까? 그러면 파싱되는 내용과 루프가 자바 측에서 실행되는 횟수를 정확히 볼 수 있습니까? 또한 테이블에 이미 항목이있을 수 있습니까? –