从PHP到SQLite的未分类保存数据

时间:2019-07-09 03:14:13

标签: android sqlite android-sqlite

我正在保存从外部服务器接收的数据。现在,这是我正在接收的示例数据:

enter image description here

现在发生的是,当我将它们保存到mysqlite db时,它没有遵循id的顺序,这是它的示例:

enter image description here

这是我的数据提取代码:

private class getData extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Http sh = new Http();
        try {
            String URLS = "http://localhost/sample.php?date="+datePHP+"&gametype="+gamePHP+"&time="+timePHP+"&bettype="+typePHP;
            String jsonStr = sh.makeServiceCall(URLS);
            if(jsonStr != null){
                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONArray sims = (JSONArray)jsonObj.get("mmi");
                for (int i = 0; i < sims.length(); i++) {
                    JSONObject c = sims.getJSONObject(i);
                    String betid = c.getString("id");
                    String betnumber = c.getString("betnumber");
                    String betamount = c.getString("betamount");
                    boolean insertBets = databaseHelper.addBets(betid, betnumber, betamount, timePHP, datePHP,gamePHP,typePHP);
                    if (insertBets){
                        System.out.println("SAVED");
                    }else{
                        System.out.println("UPDATED");
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        runOnUiThread(() -> {
            populatelistview();
        });
    }
}

保存到sqlite的代码:

public boolean addBets(String betid, String betnum, String betAmt, String betTime, String betDate, String gameType, String betType) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("betid", betid);
    contentValues.put("betNum", betnum);
    contentValues.put("betAmt", betAmt);
    contentValues.put("betTime", betTime);
    contentValues.put("betDate", betDate);
    contentValues.put("gameType", gameType);
    contentValues.put("betType", betType);

    String sql = "SELECT * FROM betsx WHERE betNum ='" + betnum + "' AND betTime ='" + betTime + "' AND betDate = '" + betDate + "' AND gameType ='" + gameType + "' AND betType ='" + betType + "'";
    Cursor cursor = db.rawQuery(sql, null);
    db.insert("betsx", null, contentValues);
    System.out.println(betnum + "//" + betAmt);
    return true;
}

0 个答案:

没有答案