2011-06-02 83 views
1

我是一名android初學者。我試圖設計這個簡單的應用程序,該應用程序使用editText View從用戶處取得城市的名稱,並將其與數據庫中的相比較,然後返回該城市的郵政編碼。 現在,我遇到了遊標執行的問題。請幫助。如何查詢數據庫以獲取相應的代碼。如何在遊戲中使用遊標搜索數據庫(sqlite查詢)android

 EditText city; 
    Button add,show1; 
    RadioGroup choose; 
    String k; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     try 
     { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     //final ContentValues values = new ContentValues(); 
     city=(EditText)findViewById(R.id.editText1); 
     add=(Button)findViewById(R.id.add); 
     choose=(RadioGroup)findViewById(R.id.radio01); 
     show1=(Button)findViewById(R.id.button1); 
     k=city.getText().toString(); 
     createDatabase(); 
    } 
     catch(Exception e) 
     { 

     } 


show1.setOnClickListener(new View.OnClickListener() { 


    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null); 
     try{ 

     if(k == "CITY") 
     { 
      String[] result_columns=new String[]{"_id","CITY","CODE"}; 
      Cursor cursor = db.query(TABLE_NAME, result_columns, 
        "CITY" +"=?", new String[]{"k"}, null, null, null); 

    cursor.moveToFirst(); 

     String xnewcode=cursor.getString(0); 
     Toast.makeText(activity1.this, xnewcode, Toast.LENGTH_LONG).show(); 
     cursor.moveToNext(); 

      //db.close(); 
     }} 
     catch(Exception e) 
     { 
      Toast.makeText(activity1.this,"Fault in showing " + e,Toast.LENGTH_LONG).show(); 

     } 

    } 

}); 
+0

具體來說,你有什麼麻煩? – 2011-06-02 13:21:33

+0

http://stackoverflow.com/questions/6218395/android-app-which-retrieves-the-zip-code-for-each-city我遇到了整個程序的問題。 http://stackoverflow.com/questions/6218395/android-app-which-retrieves-the-zip-code-for-each-city – mohit 2011-06-02 18:06:35

回答

0
Cursor mCursor = db.rawQuery("select ZIPCODE from TABLENAME where CITYNAME= ? ", new String[]{"NEWYORK"}); 

希望你只得到了一個結果。

然後從(櫃面的字符串表示)mCursor.getString(coloumn_id)

+0

這種解決方案很好..但如何擴大此查詢,以便它適用於所有的城市......併爲整個數據庫工作。 – mohit 2011-06-02 13:48:29

+0

只需從edittext中獲取字符串並將其作爲參數傳遞。 – Kakey 2011-06-02 14:01:35

+0

我在整個程序中遇到問題。這是整個程序。 http://stackoverflow.com/questions/6218395/android-app-which-retrieves-the-zip-code-for-each-city – mohit 2011-06-02 18:05:38

0

得到它,我想你忘了得到的結果在環

String[] result_columns=new String[]{"_id","CITY","CODE"}; 
     Cursor cursor = db.query(TABLE_NAME, result_columns, 
       "CITY" +"=?", new String[]{"k"}, null, null, null); 

do { 

         String xnewcode = cursor.getString(0); 
         System.ouot.println("...display the result..."+xnewcode); 
        }while (cursor.moveToNext()); 
        } 

Sample project

看看

教程sqlite database for android

謝謝 Deepak

+0

我已經使用了Toast而不是system.out.println命令。這不行嗎?我想它應該。 – mohit 2011-06-02 13:49:33

+0

這將工作。 – 2011-06-02 13:59:15

+0

http:// stackoverflow。com/questions/6218395/android-app-which-retrieve-the-zip-code-each-city我對整個程序有問題。 – mohit 2011-06-02 18:06:07

0

我認爲你忘了一些檢查。但是,這實際上取決於編寫城市的語言 - SQLite運算符「LIKE」僅對英文字母不區分大小寫。如果您的城市使用其他語言,請考慮將它們以大寫/小寫形式存儲在數據庫中,並將輸入字符串轉換爲java(String.toUpper/LowerCase())中的大寫/小寫字母。

String[] result_columns=new String[]{"_id","CITY","CODE"}; 
     Cursor cursor = db.query(TABLE_NAME, result_columns, 
       "CITY" +" like '%?%'", new String[]{"k"}, null, null, null); 

if(cursor != null) 
{ 
    while(cursor.moveToNext()) 
    { 
    String xnewcode=cursor.getString(1); 
    Toast.makeText(activity1.this, xnewcode, Toast.LENGTH_LONG).show(); 
    } 
} 

您可以將所有結果存儲在一個數組中,並顯示給用戶,要求他選擇正確的結果。編輯:如果你不熟悉,「x LIKE Y」語句將x與長度爲x.length的Y中的任何子字符串進行比較。這非常有用,因爲您無法保證用戶將輸入整個城市名稱。因此,以我的示例,如果用戶輸入「約克」,則光標將返回「紐約」,「約克郡」,「約克」等值。

+0

你可以請指導我..關於如何使用onKeyListener ..因爲用戶將在editText框中輸入城市的名稱。 – mohit 2011-06-02 17:51:04

+0

這是我的整個計劃...這是主要活動文件 – mohit 2011-06-02 17:54:29

+0

我提供了兩個活動文件..請看看它並提供幫助。 http://stackoverflow.com/questions/6218395/android-app-which-retrieves-the-zip-code-for-each-city – mohit 2011-06-02 18:04:58