我想进行查询并使用用户在edittext中输入的值,因为condition是where子句。 在数据库helper.java类中: 查询如下:
Databasehelper.java:
new Computed(1, 2)()()
在ExaminatFragment.java中:
public Cursor patientHisto ( ) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery (" select dateap,diagnostic,prestreatment from patients_table, appointments_table, examinations_table where patients_table.id = appointments_table.idp and appointments_table.idap = examinations_table.id_ap and ID = ? order by name, familyname asc" , new String[] {ExaminatFragment.value} );
return res;
}
运行应用程序并单击按钮时,会出现以下错误:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_examinat, parent, false);
myDB = new DatabaseHelper(getActivity());
id_p = (EditText)v.findViewById(R.id.id_p_text);
String value = id_p.getText().toString();
getPatHisto( );
return v;
}
public void getPatHisto( ) {
showhp.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDB.patientHisto();
if (res.getCount() == 0){
showMessage("Patient history", "No patient history found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()){
buffer.append("Dateap:"+res.getString(0)+"\n");
buffer.append("Diagnostic:"+res.getString(1)+"\n");
buffer.append("Prestreatment:"+res.getString(2)+"\n\n");
}
showMessage("Patient's history", buffer.toString());
}
}
);
}
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
请帮助..提前谢谢
答案 0 :(得分:1)
您指定为本地变量 value
,而不是分配给类成员:
String value = id_p.getText().toString();
应该是
value = id_p.getText().toString();
否则ExaminatFragment.value
将在SQL语句
null
您可能希望更改数据库方法以接受参数:
public Cursor patientHisto ( String value ) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery (" select dateap,diagnostic,prestreatment from patients_table, appointments_table, examinations_table where patients_table.id = appointments_table.idp and appointments_table.idap = examinations_table.id_ap and ID = ? order by name, familyname asc" , new String[] {value} );
return res;
}
并且不依赖于 static 字段来传递该值。您对该方法的调用将更改为
Cursor res = myDB.patientHisto(value);