向Toast显示查询

时间:2012-11-28 08:19:40

标签: android sqlite android-listview

我尝试从我的数据库中选择与我在列表视图中点击的项目相匹配的特定字段,当我运行应用程序并单击应用程序崩溃的任何项目时,我的代码出了什么问题?

以下是logcat的错误

11-28 16:08:50.168: E/AndroidRuntime(27284): FATAL EXCEPTION: main
11-28 16:08:50.168: E/AndroidRuntime(27284): android.database.sqlite.SQLiteException: near "General": syntax error: , while compiling: SELECT Region,City,Address,Contact FROM tblHospitals WHERE HospitalName = Bernardino General Hospital
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at com.dr.droid.lee.Health.onListItemClick(Health.java:54)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.app.ListActivity$2.onItemClick(ListActivity.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.widget.AbsListView.performItemClick(AbsListView.java:1071)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2527)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.widget.AbsListView$1.run(AbsListView.java:3181)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.os.Handler.handleCallback(Handler.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.os.Handler.dispatchMessage(Handler.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.os.Looper.loop(Looper.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at android.app.ActivityThread.main(ActivityThread.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at java.lang.reflect.Method.invokeNative(Native Method)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at java.lang.reflect.Method.invoke(Method.java:511)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
11-28 16:08:50.168: E/AndroidRuntime(27284):    at dalvik.system.NativeStart.main(Native Method)

这是我的班级文件

public class Health extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.health);
        displayHospitals();
        }

    public void displayHospitals (){
        final ListView lv = getListView();
        DbHelper tblHL = new DbHelper(this);
        tblHL.open();
        ArrayList<String> result = tblHL.getHData();
        result = tblHL.getHData();
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, result));
        lv.setTextFilterEnabled(true);

         tblHL.close();
         }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String hosp = (String)getListAdapter().getItem(position);
        String quer = "SELECT Region,City,Address,Contact FROM tblHospitals WHERE HospitalName = " +hosp ;
        SQLiteDatabase mydb = openOrCreateDatabase("dbDrDroid", MODE_PRIVATE, null);
        Cursor c = mydb.rawQuery(quer, null);
        c.moveToFirst();
        Toast.makeText(this, c.toString(), Toast.LENGTH_SHORT).show();
        }


}

2 个答案:

答案 0 :(得分:1)

我认为您需要这样做,以确保您的查询正确处理医院名称中的空格:

String quer = "SELECT Region,City,Address,Contact FROM tblHospitals WHERE HospitalName = \"" +hosp + "\"";

答案 1 :(得分:1)

您必须更改以下行

String quer = "SELECT Region,City,Address,Contact FROM tblHospitals WHERE HospitalName = '" +hosp+"'" ;

因为HospitalName是文字