在插入时,我遵循了这个人的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();
}
}
但是下面它给了我这个错误消息,我想知道为什么或为什么是错误的,因为我的查询是一个没有截断的插入。请帮助
答案 0 :(得分:0)
首先,我建议您使用 PreparedStatement 设置参数以避免 SQL注入
对于您的问题,原因是id
是integer
的类型,但是您只是为此而通过了''
:
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')";