JSON数组循环插入Android SQLite数据库
我有以下片段,循环遍历 JSON 数组并将数据插入数据库。我的问题是它没有插入所有记录。如果我有 6 条记录,它只会插入 6 条记录中的 5 条。如果我有 5 条记录,它只会插入 4 条?当我调试storesArray.length时,它显示数组中的总记录为6。我做错了什么,它不会循环并插入数组中的所有记录?
以下是从服务器传入的 SQL 字符串:
[{"id":"29","cId":"3","sName":"Milford Plaza Hotel","sNumber":"3061","sCustNm":"VS7997","sSalesman":"1234","sAddress":"","sContact":"","sPhone":""},{"id":"26","cId":"7","sName":"Circle K Bowling Green","sNumber":"5670","sCustNm":"VS7976","sSalesman":"1234","sAddress":"1091 N Main Street\r\nBowling Green, Ohio 43402","sContact":"","sPhone":""},{"id":"25","cId":"7","sName":"Circle K Bowling Green","sNumber":"5669","sCustNm":"VS7975","sSalesman":"1234","sAddress":"966 S Main Street\r\nBowling Green, OH 43402","sContact":"","sPhone":""},{"id":"22","cId":"6","sName":"Rock Branch Exxon","sNumber":"122","sCustNm":"VS7983","sSalesman":"1234","sAddress":"Rt 1 Box 34 A\r\nPoca, W.V.","sContact":"","sPhone":""},{"id":"23","cId":"6","sName":"Marathon Food Mart","sNumber":"156","sCustNm":"VS7984","sSalesman":"1234","sAddress":"2300 Pike Street\r\nParkerburg, W.V.","sContact":"Jenny","sPhone":""},{"id":"27","cId":"7","sName":"Circle K Dayton","sNumber":"5346","sCustNm":"VS7992","sSalesman":"1234","sAddress":"816 Waterveilt Ave\r\nDayton, OH 45420","sContact":"","sPhone":""}]
JSONArraystoresArray = new JSONArray(results);
for(int i = 0; i < storesArray.length(); i++){
JSONObject store = storesArray.getJSONObject(i);
db.execSQL("INSERT INTO stores ( sId, cId, sName, sAddress, sNumber, sSalesman, sCustNum, sPhone ) " +
"VALUES ('"+store.getString("id")+"', '"+store.getString("cId")+"', '"+store.getString("sName")+"', '"+store.getString("sAddress")+"', '"+store.getString("sNumber")+"', '"+store.getString("sSalesman")+"', '"+store.getString("sCustNm")+"', '"+store.getString("sPhone")+"' )");
}
I have the following snippet the loops through a JSON Array and inserts the data into a database. My problem is that it's not inserting all the records. If I have 6 records it will only inserts 5 of the 6. If I have 5 records it will only insert 4? When I debug the storesArray.length it shows the total records in the array as 6. What I'm I doing wrong that it won't loop and insert all the records in the array?
Here's the SQL String that's coming in from the server:
[{"id":"29","cId":"3","sName":"Milford Plaza Hotel","sNumber":"3061","sCustNm":"VS7997","sSalesman":"1234","sAddress":"","sContact":"","sPhone":""},{"id":"26","cId":"7","sName":"Circle K Bowling Green","sNumber":"5670","sCustNm":"VS7976","sSalesman":"1234","sAddress":"1091 N Main Street\r\nBowling Green, Ohio 43402","sContact":"","sPhone":""},{"id":"25","cId":"7","sName":"Circle K Bowling Green","sNumber":"5669","sCustNm":"VS7975","sSalesman":"1234","sAddress":"966 S Main Street\r\nBowling Green, OH 43402","sContact":"","sPhone":""},{"id":"22","cId":"6","sName":"Rock Branch Exxon","sNumber":"122","sCustNm":"VS7983","sSalesman":"1234","sAddress":"Rt 1 Box 34 A\r\nPoca, W.V.","sContact":"","sPhone":""},{"id":"23","cId":"6","sName":"Marathon Food Mart","sNumber":"156","sCustNm":"VS7984","sSalesman":"1234","sAddress":"2300 Pike Street\r\nParkerburg, W.V.","sContact":"Jenny","sPhone":""},{"id":"27","cId":"7","sName":"Circle K Dayton","sNumber":"5346","sCustNm":"VS7992","sSalesman":"1234","sAddress":"816 Waterveilt Ave\r\nDayton, OH 45420","sContact":"","sPhone":""}]
JSONArray storesArray = new JSONArray(restults);
for(int i = 0; i < storesArray.length(); i++){
JSONObject store = storesArray.getJSONObject(i);
db.execSQL("INSERT INTO stores ( sId, cId, sName, sAddress, sNumber, sSalesman, sCustNum, sPhone ) " +
"VALUES ('"+store.getString("id")+"', '"+store.getString("cId")+"', '"+store.getString("sName")+"', '"+store.getString("sAddress")+"', '"+store.getString("sNumber")+"', '"+store.getString("sSalesman")+"', '"+store.getString("sCustNm")+"', '"+store.getString("sPhone")+"' )");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜
store.getString("id")
应该是您的主列 ID?您不应该在插入查询中定义它,数据库会为您执行此操作。更重要的是:不要将字符串直接放入查询中,因为这是破坏查询的好方法(称为 SQL 注入),
请执行以下操作:
I guess
store.getString("id")
should be your primary column ID? You shouldn't define that in an insert query, the database will do that for you.More important: Don't put strings directly in your query since this is a great way to destroy your query (called SQL injection)
Do something like that: