我在csv文件中存储了一些数据。当我在绿色道中复制此数据时,带有反逗号符号的数据会给出 SQLite 异常
Caused by: android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: INSERT INTO CATEGORIES (_id, SNO, SINDHI, HINDI ,ENGLISH, DRAWABLE, AUDIO, CATEGORY) values(188,9,'सस्','सास','Wife's or Husband's Mother','R.drawable.blank','R.raw.saas','Relatives');
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
我想要那个符号,所以我不能在文件中进行任何更改。请帮助将该符号存储在绿色道中。这是我的一些代码:
InputStream inputStream = getResources().openRawResource(R.raw.animals);
InputStreamReader csvStreamReader = new InputStreamReader(inputStream);
BufferedReader buffer = new BufferedReader(csvStreamReader);
String tableName = "CATEGORIES";
String columns = "_id, SNO, SINDHI, HINDI ,ENGLISH, DRAWABLE, AUDIO, CATEGORY";
String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
String str2 = ");";
SQLiteDatabase db;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "category-db", null);
db = helper.getWritableDatabase();
db.beginTransaction();
String line;
int count=0;
try {
while ((line = buffer.readLine()) != null) {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(",");
sb.append(count + ",");
sb.append(str[0] + ",");
sb.append("'" + str[1] + "',");
sb.append("'" + str[2] + "',");
sb.append("'" + str[3] + "',");
sb.append("'" + str[4] + "',");
sb.append("'" + str[5] + "',");
sb.append("'" + str[6] + "'");
sb.append(str2);
db.execSQL(sb.toString());
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
摆脱字符串串联并使用?
参数占位符以及传递给execSQL()
的参数数组。
或者,使用 insert()
方法。
或者,转义单引号,使用 ''
而不是 '
。