0

I am storing some data in csv files. When i am copying this data in green dao then the data with inverted comma symbols are giving SQLite Exception

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)

I want that symbols so i can not make any changes in the file. Please help to store that symbols in greendao. And here is some of my code :

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();

1 Answers1

1

Get rid of the string concatenation and use ? parameter placeholders plus the parameters array passed into execSQL().

Or, use the insert() method.

Or, escape the single quotes, using '' instead of \'.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491