从json插入数据进行查询时发生sqllite异常?

时间:2018-09-20 09:18:48

标签: android json sqlite exception

我正在尝试从Json向sqllite TABLE添加一些数据。在我的代码下面,用于从json数据下载数据。并具有一个用于执行我的查询的外部sqllite类。 dbclass是sqllite类的对象,并调用dbclass.setter方法来执行我的查询

     JSONArray item_uom_list = object.getJSONArray("ItemUOM");
                        if (item_uom_list.length() > 0) {
                            dbClass.setterValues("delete from tbl_item_uom");
                            for (int i = 0; i < item_uom_list.length(); i++) {
                                JSONObject item_uom_object = item_uom_list.getJSONObject(i);
                                String a = item_uom_object.getString("Org_Id");
                                String b = item_uom_object.getString("Item_No");
                                String c = item_uom_object.getString("Code");
                                String d = item_uom_object.getString("Barcode");
                                String e = item_uom_object.getString("UOMDescription");
                                String f = item_uom_object.getString("UOMQty");
                                String g = item_uom_object.getString("AutoPrice");

                                /*String sql="insert into tbl_item_uom values('?','?','?','?','?','?','?')";
                                dbClass.setterValues(sql,new String[] {a,b,c,d,e,f,g});*/


                                dbClass.setterValues("insert into tbl_item_uom values('" + a + "','" + b + "','" + c + "','" + d + "','" + e + "','" + f + "','" + g + "')");
                            }
                        }

我有外部sqllite类, 我的logcat在

以下
                          09-20 14:43:17.670 19871-19871/com.krishna.glowis E/SQLiteLog: (1) near "s": syntax error 09-20 14:43:17.674 19871-19871/com.krishna.glowis D/AndroidRuntime: Shutting down VM 09-20 14:43:17.677 19871-19871/com.krishna.glowis E/AndroidRuntime: FATAL EXCEPTION: main Process: com.krishna.glowis, PID: 19871  android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: insert into tbl_items values('293','PR111','DIAPER-BE SURE-M 5's','','','','GST@12%','','','1','I','1','','1.0','1.0','10.0','1','411000','131000','449300','511100','442000','N','','1','1','null','Item','2018-08-01 04:33:44.95','N','N') at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
    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:1704)
    atandroid.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1635)
    at Support_Classes.DBClass.setterValues(DBClass.java:332)
    at Fragments.Synch_Data$3.onResponse(Synch_Data.java:462)
    at Fragments.Synch_Data$3.onResponse(Synch_Data.java:302)
    at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)

2 个答案:

答案 0 :(得分:1)

请查看您的日志,第一行告诉您您的问题。当您尝试使用值运行查询时:

repository

您用撇号将字符串断开。在需要的地方使用两个撇号。

..., 'DIAPER-BE SURE-M 5's', ...

不是双(“)撇号,只有两个单打(')

答案 1 :(得分:1)

  

android.database.sqlite.SQLiteException:“ s”附近:语法错误(代码   1):

A SQLite exception that indicates there was an error with SQL parsing or execution. 

您可以尝试使用ContentValues

演示

SQLiteDatabase database = this.getWritableDatabase();
for (int i = 0; i < item_uom_list.length(); i++) 
{
    JSONObject item_uom_object = item_uom_list.getJSONObject(i);
    String a = item_uom_object.getString("Org_Id");
    String b = item_uom_object.getString("Item_No");
    String c = item_uom_object.getString("Code");
    String d = item_uom_object.getString("Barcode");
    String e = item_uom_object.getString("UOMDescription");
    String f = item_uom_object.getString("UOMQty");
    String g = item_uom_object.getString("AutoPrice");


        ContentValues values = new ContentValues();
        values.put("Org_Id", a); 
        values.put("Item_No", b); 
        .......................
        ......................
        values.put("AutoPrice", g); 
        database.insert("tbl_item_uom", null, values);
}

database.close();