代码之家  ›  专栏  ›  技术社区  ›  Janarthan Thumburu

如何从android解析数据库中检索特定行数据

  •  0
  • Janarthan Thumburu  · 技术社区  · 7 年前

    我的问题是我需要根据设备检索点。我正在从电话管理器获取设备id。但是,如何根据设备id进行查询以检索点,如果没有找到行,则显示在数据库中添加一个带有零点的行。

    1 回复  |  直到 7 年前
        1
  •  1
  •   letsCode    7 年前

    如果将用户放在同一个表中,则可以执行此操作。

    ParseQuery<ParseObject> query = ParseQuery.getQuery("PointsCollected");
        query.whereEqualTo("currentUser", currentUset.getObjectId());
        // this will find the user.
        // then find the first instance
        query.findFirstInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> object, ParseException e) {
                if (e == null) {
                    textView.setText(object.getString("device"));
                } else {
                    Log.d(TAG, "Error: " + e.getMessage());
                }
            }
        });
    

    这应该行得通。

        // At the end we want to get the points.
        final int points; 
    
        //First, lets get the ID of the device and store that into a veriable.
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        final String deviceId = telephonyManager.getDeviceId().toString();
    
        //Then lets query the PointsCollected database.
        ParseQuery<ParseObject> query = ParseQuery.getQuery("PointsCollected");
        // Then we want to see where the DeviceID (from the variable) matches the device from the database.
        // "device" is the row in the database.
        // deviceID is the varible that is storing that users device ID.
        query.whereEqualTo("device", deviceId);
        //Then we get the first instance...
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                 // you want to do a check on the ParseException here as well. 
                if (object == null) {
                    Log.d("nothing found", "let's go ahead and create a new object.");
                    //ADD THE OBJECT AS A NEW OBJECT!!!!
                } else {
    
                    points = object.getInt("pointsObtained");
                    Log.d("points found", points + "");
                }
            }
        });