2012-08-24 52 views
0

我目前正試圖從我自己的數據庫中找到一個單獨的行,位於資產文件夾中。檢查表中是否有行,SQLite查詢瀏覽器和調試器的SELECT語句讓我在數據庫中有一行。無論如何,從我的代碼調用聲明只是讓我一個空的光標。我還添加方法Android遊標返回沒有行的選擇查詢

cursor.moveToFirst()

和避免呼叫

myDataBase.close();

這裏就是我想要追加對一個TextView單個條目的方法:

private void getAdresses(Location location) { // TODO 
    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    Geocoder geocoder = new Geocoder(this, Locale.GERMAN); 

    try { 
     List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 
     if (addresses.size() != 0) { 
      Address address = addresses.get(0); 

      String addressString = address.getAddressLine(0).toString(); 
      System.out.println("(geocoder) " + addressString); 
      textView.append(addressString + "\n"); 

      Cursor cursor = mDbHelper.getAllEntrys("tblBar", "Adresse", 
        addressString); 

      if (cursor.moveToFirst()) { 

       String cursorContent = cursor.getString(cursor 
         .getColumnIndexOrThrow("Name")); 
       textView.append(cursorContent + "\n"); 

      } else { 
       System.out.println("No bar at current location"); 
      } 
     } else { 
      System.out.println("(geocoder) no address!"); 
     } 
    } catch (IOException e) { 
     System.out.println(e.toString() + "(geocoder)"); 
    } 
} 

而且這是我調用數據庫的實際查詢方法:

public Cursor getAllEntrys(String table, String column, String search) { 
    try { 
     String sSelect = "SELECT * FROM " + table + " WHERE " + column 
       + " = '" + search + "'"; 
     Cursor cursor = mDb.rawQuery(sSelect, null); 

     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 
     return cursor; 
    } catch (SQLException mSQLException) { 
     System.out.println("getData >>" + mSQLException.toString()); 
     throw mSQLException; 
    } 
} 

希望有人能幫助我。提前謝謝了!

+0

嘗試調用DatabaseUtils.dumpCursor(光標)後,立即光標光標= mDb.rawQuery(S選擇,NULL);並檢查logcat的轉儲。它會告訴你遊標是否有任何內容。 – Samuel

+0

感謝您的快速回答。我試着轉儲光標,它告訴我,我的光標沒有內容。問題是我不知道爲什麼。 – user1622601

+0

首先確保數據庫連接沒有問題。然後在運行查詢之前記錄sSelect。這將幫助您在查詢中發現錯誤。您應該可以將日誌消息正確複製到SQLite客戶端中,而不會出現任何問題。 – Samuel

回答

2

好的傢伙首先我想感謝你們所有人的快速回復。保持!雖然答案很明顯,但我自己想到了。本教程中的問題來自:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/,它不考慮您在第一次運行程序之後手動添加值(SQLite數據庫瀏覽器中的更新)到數據庫的情況。那就是我所做的......所以你必須編寫自己的方法來檢查自上次運行以來是否有任何更改。希望任何人都可以需要這個:)

0

試試從數據庫中獲取單行。

在你的數據庫類中聲明這個方法。

public Cursor selectSingleRecord(String tableName, String[] tableColumns, 
     String whereClause, String whereArgs[], String groupBy, 
     String having, String orderBy) { 
    return myDataBase.query(tableName, tableColumns, whereClause, whereArgs, 
      groupBy, having, orderBy); 

} 

並使用此方法獲取遊標。

如果我的表格結構在下面。

_id |名稱|地址

我將在下面的方式

String tableColumns[] = {"_id"}; // Here mansion the column which you want to retrieve I think in your case "addressString". 
    String whereClause = {"Name"}; // Here use where clause 

    Cursor cursor = dbHelper.selectSingleRecord("Your_Table_Name", tableColumns, whereClase +"=?" , new String[] {String.valueOf("NameOfPerson")}, null, null, null); 

    // NameOfperson is the name which I am going to enter through EditText. 
     if (cursor.moveToFirst()) 
     {    
       String getId = cursor.getString[0]; // Here you can get the id of respective person (i.e. the name which I have entered)       
     } 
     cursor.close(); // Finally close the curser 

希望這將幫助你寫。

+0

謝謝。我嘗試過,但它不適合我。仍然得到一個空的遊標。 – user1622601

+0

你能告訴我更新的代碼嗎? – Akshay

+0

String [] columns = {「_id」,「Name」,「Adresse」,「Price_Beer」,「Price_Liquor」,「Price_Wine」,「Longitude」,「Latitude」};遊標cursor = mDbHelper.selectSingleRecord(「tblBar」,columns,「Adresse」+「=?」,new String [] {String.valueOf(addressString)},null,null,null);希望你能以這種方式閱讀它,或者我應該把它發佈在一個新的答案?另外我添加了我的Databaseclass中的方法selectSingleRecord,就像你發佈它的方式一樣。 – user1622601

0

將您的s選擇更改爲:有時在SQL中最好使用「something」而不是= something。

String sSelect =「SELECT * FROM」+ table +「WHERE」+ column +「like」「+ search +」'「;

0

不知道這可能是問題,但。 。 。

String sSelect = "SELECT * FROM " + table + " WHERE " + column 
       + " = '" + search + "'"; 

當您搜索變量爲空,您的查詢翻譯爲:

SELECT * FROM tblBar WHERE住址= '空'

嘗試改用此:

search = (search == null) ? " IS NULL " : " = '" + search +" '" ; 

String sSelect = "SELECT * FROM " + table + " WHERE " + column + search; 
+0

謝謝,但那不是問題我得到我從地理編碼器對象搜索的地址,並且在執行查詢之前我檢查給定的字符串是否爲空 – user1622601

1

//這對我的作品

final Cursor c = sampleDB.rawQuery("SELECT FirstName, LastName,RedId FROM " + 
       SAMPLE_TABLE_NAME + 
       " where RedId = '"+a+"'", null); 
     int count = c.getCount(); 

     if(count == 0){ 

      Toast.makeText(RetrieveData.this, "Not Found", Toast.LENGTH_SHORT).show(); 
     }