在插入时,我遵循了此人的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();
}
}
但是,它在下面给了我这个错误消息,我想知道为什么或为什么是错误的,因为我的查询是一个不被截断的插入。请帮忙
最佳答案
首先,我建议您使用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')";