在插入时,我遵循了此人的answer
这是我的AsyncTask的代码

    private class sendRequest extends AsyncTask<String, String, String> {

    String z = "";

    boolean isSuccess = false;

    @Override
    protected void onPreExecute() {
        progressDialog.setMessage("Sending request...");
        progressDialog.show();

        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Please check your internet connection";
            } else {

                myArrayList.clear();

                String query = "insert into notifications (id, suggestion, type, isIgnored) values ('', '"+ sv.getQuery().toString()  +"', 'medicine', 'false')";

                Statement stmt1 = con.createStatement();

                stmt1.executeUpdate(query);

            }
        } catch (Exception ex) {
            isSuccess = false;
            z = "Exceptions" + ex;
        }
        return z;
    }

    @Override
   protected void onPostExecute(String s) {
        if(!isSuccess && !z.equals("")) {
            Toast.makeText(getBaseContext(), z, Toast.LENGTH_LONG).show();
        }

        progressDialog.hide();
    }
}




但是,它在下面给了我这个错误消息,我想知道为什么或为什么是错误的,因为我的查询是一个不被截断的插入。请帮忙

android - 在Android中使用JDBC连接在MySQL上插入行的问题-LMLPHP

最佳答案

首先,我建议您使用PreparedStatement设置参数以避免SQL注入

对于您的问题,原因是idinteger的类型,但是您只是为其传递了''

 String query = "insert into notifications (id, suggestion, type, isIgnored) values ('', '"+ sv.getQuery().toString()  +"', 'medicine', 'false')";


有两种解决方法:
一种。传递id的整数值
b。使id为自动增量,并在sql中删除id,如下所示:

String query = "insert into notifications (suggestion, type, isIgnored)
         values ('"+ sv.getQuery().toString()  +"', 'medicine', 'false')";

08-18 00:29
查看更多