0

我正在尝试从 Android Activity 创建一个数据库,当我在表中添加一个新项目时它可以工作。但是,当我尝试更新该项目时,我遇到了几个问题,这不起作用。数据库的结构如下:pid、name、price和description。为了创建数据库,我将一些参数发布到有一些脚本的服务器中。为了创建产品,我使用以下查询:mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");这很好用。但是,当我尝试更新时,我使用

 mysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description' WHERE name = $name"); but this gives a JSON error.

我曾想过只用这样的 pid 更新项目mysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description' WHERE pid = $pid");,但我不知道在 Android 中由谁来检索 pid 的值。我需要说 pid 归档是 Autoincrement。

编辑:

class CreateNewProduct extends AsyncTask<String, String, String> {

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(NumericoSup.this);
                    pDialog.setMessage("Creating Product..");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

                /**
                 * Creating product
                 * */
                protected String doInBackground(String... args) {


                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("name", name1));
                    params.add(new BasicNameValuePair("price", "88"));
                    params.add(new BasicNameValuePair("description", "numericopr"));

                    // getting JSON Object
                    // Note that create product url accepts POST method
                    JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                            "POST", params);

                    // check log cat fro response
                    Log.d("Create Response", json.toString());

                    // check for success tag
                    try {
                        int success = json.getInt(TAG_SUCCESS);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }

                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog once done
                    pDialog.dismiss();
                }

            }   
            class SaveProductDetails extends AsyncTask<String, String, String> {

                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(NumericoSup.this);
                    pDialog.setMessage("Saving product ...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

                /**
                 * Saving product
                 * */
                protected String doInBackground(String... args) {

                    // getting updated data from EditTexts


                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair(TAG_PID, "20"));
                    params.add(new BasicNameValuePair(TAG_NAME, name1));
                    params.add(new BasicNameValuePair(TAG_PRICE, "99"));
                    params.add(new BasicNameValuePair(TAG_DESCRIPTION, "update"));

                    // sending modified data through http request
                    // Notice that update product url accepts POST method
                    JSONObject json = jsonParser.makeHttpRequest(url_update_product,
                            "POST", params);

                    // check json success tag
                    try {
                        int success = json.getInt(TAG_SUCCESS);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }


                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog once product uupdated
                    pDialog.dismiss();
                }
            }

Thanks in advance 
4

0 回答 0