user1622601
user1622601

Reputation: 21

Android cursor returns no rows for select-query

i'm currently trying to get a single row from my own database, located in the assets folder. Checking if there is a row in the table, with the SQLite Query Browser and the SELECT-Statement from the debugger got me a single row in the database. Anyway calling the statement from my code just gets me an empty cursor. I also added the method

cursor.moveToFirst()

and avoided calling

myDataBase.close();

Here is the method where i'm trying to append a single entry on a 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)");
    }
}

Furthermore this is my method for calling the actual query on the database:

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;
    }
}

Hope someone can help me with this. Many thanks in advance!

Upvotes: 0

Views: 7978

Answers (5)

Mwasika
Mwasika

Reputation: 11

//This works for me

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();
        }

Upvotes: 1

Akshay
Akshay

Reputation: 2534

Try this for getting single row from your database.

Declare this method in your Database class.

 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);  

} 

And use this method to get the cursor.

If my table structure is below.

_id | Name | Address

I will write in the following way

    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

Hope this will help you.

Upvotes: 0

user1622601
user1622601

Reputation: 21

Okay guys first i want to thank all of you for your quick responses. Keep it up! Although the answer is quite obvious, i figured it out myself. The problem in the tutorial from: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ is, that it doesn't consider the case that you add values manually (Updates in the SQLite Database Browser) to your database after you first run the programm. Thats what i did... So you have to write your own method to check if there were any changes since the last run. Hope anyone can need this :)

Upvotes: 2

s.d
s.d

Reputation: 29436

Don't know if this might be the problem but . . .

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

When your search variable is null, your query translates to :

SELECT * FROM tblBar WHERE Adresse = 'null'

try to use this instead :

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

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

Upvotes: 0

luoisbeck
luoisbeck

Reputation: 1

Change your sSelect to this: Sometimes in SQL it's better to use like 'something' than = something.

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

Upvotes: 0

Related Questions