将数据从 json 插入查询时发生 Sqlite 异常?

Posted

技术标签:

【中文标题】将数据从 json 插入查询时发生 Sqlite 异常?【英文标题】:Sqllite exception occur while inserting data from json to query? 【发布时间】:2019-02-24 13:25:36 【问题描述】:

我正在尝试将一些数据从 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)

【问题讨论】:

有人说你不能直接插入数据,我不知道如何插入我的数据 查找准备好的语句。 @juergend 请分享一个有用的链接或解决我的问题 来吧。只需谷歌 Android sqlite 准备语句 【参考方案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();

【讨论】:

【参考方案2】:

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

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

你用撇号断开字符串。在需要它们的地方使用两个撇号。

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

不是双撇号 ("),只是两个单撇号 (')

【讨论】:

为了记录,停止使用 a, b, c, d, ... 作为变量名,这是灾难的根源。另外,不要用 ...+ a + '"、"' + b... 连接字符串...至少使用字符串格式...这会有很大帮助 ;) 谢谢你,但现在还不能解决:android.database.sqlite.SQLiteException: near "pen": syntax error (code 1): , while compile: insert into tbl_items values(293, 00001,luxy pen,blue lexy,,4365,GST@5,,,1,I,18,,0.225,0.0,1.274,0,411000,131000,449300,511101,442000,N,,2,35,null ,项目,2018-08-19 06:42:25.453,'N','N') @ShameemAhsan 对不起,误读了。在这种情况下,您没有在字符串上使用撇号。 @Antonio 是的,我现在不在我的代码上使用撇号。该怎么做 使用它们:)。你必须使用它们,你只需要确保当你将它们放在你的字符串中时,你将它们加倍,否则它会断裂。

以上是关于将数据从 json 插入查询时发生 Sqlite 异常?的主要内容,如果未能解决你的问题,请参考以下文章

使用 SQLite API 将 API 输出 (JSON) 插入 SQLite

使用 Gson 解析后将嵌套的 JSON 数组插入 SQLite

SQLite 插入查询无法正常工作

批量插入不插入

如何将大型 JSON 数据从服务器存储到 SQLITE Android?

带有 FMDB 包装器的 Sqlite 数据库不插入数据