1

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

这是来自 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();
        }


}
4

2 回答 2

1

您必须更改以下行

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

因为HospitalName是文本

于 2012-11-28T08:29:44.547 回答
1

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

String quer = "SELECT Region,City,Address,Contact FROM tblHospitals WHERE HospitalName = \"" +hosp + "\"";
于 2012-11-28T08:26:50.357 回答