从Mysql在Sqlite中存储多个数据

时间:2014-12-12 10:29:44

标签: php android mysql sqlite

我想将特定用户的数据从mysql存储到sqlite。我只能接收一行特定用户。

这是我的代码。

登录

JSONObject jsonexercise = userFunction.exerciseItems(name);

try {
    System.out.println(" exe try");
    if (jsonexercise.getString(KEY_SUCCESS) != null) {
        String result = json.getString(KEY_SUCCESS); 
        System.out.println(" exersise result" + result);
        if(Integer.parseInt(result) == 1){
            System.out.println("exercise inside parse");
            JSONObject json_exercisedata = json.getJSONObject("getexercise");
            db.exerciseItem(json_exercisedata.getString(KEY_ENAME),json_exercisedata.getString(KEY_EXERCISE_NAME),json_exercisedata.getString(KEY_DURATION),json_exercisedata.getString(KEY_CALORIE_BURNED),json_exercisedata.getString(KEY_ECREATED_AT));
        }
    } else {
        System.out.println("no exercise item to show");
    }
}
catch (JSONException e) {
System.out.println("no exerc item to show");

}

userfunction

public JSONObject exerciseItems(String name){
    // Building Parameters

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", get_exetag));

    params.add(new BasicNameValuePair("name", name));
    System.out.println("in json object name" + name);

    // getting JSON Object
    JSONObject jsonexercise = jsonParser.getJSONFromUrl(insertURL, params);
    // return json
    return jsonexercise;
}

dbhandler

public void exerciseItem(String name, String exercisename,String duration, String calorieburned, String created_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    System.out.println("in db handler insert user the name is" + name);
    values.put(KEY_EXERCISENAME, exercisename); // Email
    values.put(KEY_DURATION, duration); 
    values.put(KEY_CALORIE_BURNED, calorieburned);
    values.put(KEY_CREATED_AT, created_at); // Created At
    // Inserting Row
    db.insert(EXERCISE_ITEM, null, values);
    db.close(); // Closing database connection
}

PHP

public function getExeciseitems($name) {
    $result = mysql_query("SELECT name,exercisename,duration,calorieburned,created_at FROM exercisedata WHERE name = '$name'") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysql_fetch_array($result);
        return $result;
    } else {
        // user not found
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);

                return $result;

        } else {
            // user not found
            return false;
        }

不起作用,因为您提取了多个记录。 你需要像这样循环:

if ($no_of_rows > 0) {
while ($row = mysql_fetch_array($result)){
 $result[] = $row;
}
return $result;
}else {
                // user not found
                return false;
            }

代码未经测试。

修改:为避免此错误此链接PHP - cannot use a scalar as an array warning 说使用:

if ($no_of_rows > 0) {
    $i = 0;
    $result = array();
    while ($row = mysql_fetch_array($result)){
     $result[$i] = array();
     $result[$i] = $row;
     $i++;
    }
    return $result;
    }else {
                    // user not found
                    return false;
                }

我添加了一个用于填充的计数器。 ($ⅰ)

edit2:更改了名称:

if ($no_of_rows > 0) {
        $i = 0;
        $result2 = array();
        while ($row = mysql_fetch_array($result)){
         $result2[$i] = array();
         $result2[$i] = $row;
         $i++;
        }
        return $result2;
        }else {
                        // user not found
                        return false;
                    }