gpt4 book ai didi

java - 如何在csv文件中存储引号,然后将该数据复制到android中的greendao

转载 作者:行者123 更新时间:2023-12-01 09:42:04 25 4
gpt4 key购买 nike

我正在 csv 文件中存储一些数据。当我在 green dao 中复制此数据时,带有引号的数据给出 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)

我想要这些符号,这样我就无法在文件中进行任何更改。请帮助将该符号存储在 greendao 中。这是我的一些代码:

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() 方法。

或者,escape the single quotes, using '' instead of \' .

关于java - 如何在csv文件中存储引号,然后将该数据复制到android中的greendao,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com