我想将csv(逗号分隔)文件导入sqlite。我将资产文件夹中的csv文件保存,然后插入表...我必须将文件保存为UTF-8格式。 当我保存没有UTF-8格式的CSV文件时,我的应用程序运行没有任何问题但是当我在excel中保存相同的CSV文件然后以utf-8格式保存然后设置在资产文件夹时,在插入期间,我得到以下错误:
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
这是我的代码:
@Override
public void onCreate(SQLiteDatabase db) {
//db=context.openOrCreateDatabase(sDB_Name,Context.MODE_PRIVATE,null);
db.execSQL(sCreate_Table);
createDBFromCSV(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + MASKANLOC_TABLE);
onCreate(db);
}
private void createDBFromCSV(SQLiteDatabase db) {
AssetManager assetManager = context.getAssets();
try {
InputStream inputStream = assetManager.open(maskanFile);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String column_value[];
String insert_row = "insert into " + MASKANLOC_TABLE + " (" + COLUMN_ID + "," + COLUMN_LAT + "," + COLUMN_LONG + "," + COLUMN_ADDRESS + "," + COLUMN_CODE + "," + COLUMN_TYPE + "," + COLUMN_UPLOADED + ")" + " values(\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")";
while ((line = bufferedReader.readLine()) != null) {
column_value = line.split(";");
String setCommand = String.format(insert_row, column_value[0], column_value[1], column_value[2], column_value[3], column_value[4], column_value[5], column_value[6]);
db.execSQL(setCommand);
}
} catch (IOException e) {
e.printStackTrace();
}
}
感谢您的建议