User210282
User210282

Reputation: 83

SQLite query execution crashes Android App

I am populating a listview from SQLite database, and the app crashes when I click on an item. I have done enough research using "query" and "rawquery". Also successfully executed rawquery statement in SQLite Database Browser, but still the same statement doesn't work in my code.

Below is the code of my listview onitemclicklistener:

//  Bookmark ListView Listener
    bookmarkListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent1, View view1, int position1,
                long id1) {
            String selectedBookmarkItem = ((TextView)view1).getText().toString();

            dbAdapter.read();

            Map<String, Object> bookmarkKV = dbAdapter.getSingleBookmark(selectedBookmarkItem);
            String bookmarkedURL = (String) bookmarkKV.get(dbAdapter.BOOKMARK_ADDRESS);

            dbAdapter.close();
            Toast.makeText(getBaseContext(), bookmarkedURL, Toast.LENGTH_LONG).show();
}
});

Method getSingleBookmark code:

//  Fetch Single Bookmark
public Map<String, Object> getSingleBookmark(String bookmarkName1) throws SQLException{
    Map<String, Object> map1 = new HashMap<String, Object>();
    Cursor cursor1 = db.query(true, BOOKMARKS_TABLE, new String[] {BOOKMARK_ADDRESS},
            BOOKMARK_NAME + "=" + bookmarkName1, null, null, null, null, null);

    if(cursor1 != null){
        cursor1.moveToFirst();
    }

    String bookmarkAddress = cursor1.getString(cursor1.getColumnIndex(BOOKMARK_ADDRESS));
    map1.put(BOOKMARK_ADDRESS, bookmarkAddress);
    //String bookmarkName = cursor1.getString(cursor1.getColumnIndex(BOOKMARK_NAME));
    //map1.put(BOOKMARK_NAME, bookmarkName);

    return map1;
}

LogCat entry:

09-30 03:43:12.791: E/AndroidRuntime(29008): FATAL EXCEPTION: main
09-30 03:43:12.791: E/AndroidRuntime(29008): android.database.sqlite.SQLiteException:     
no such column: gmail (code 1): , while compiling: SELECT DISTINCT bookmarkaddress 
FROM bookmarkstable WHERE bookmarkname=gmail

When I click on "gmail" item in listview, the app crashes saying "Unfortunately, App has stopped".

But the database table has the row with "gmail" entry:

enter image description here

Upvotes: 0

Views: 1758

Answers (2)

tomieq
tomieq

Reputation: 29

Always when using raw query you must surround strings with quote and also escape quotas in your string (replace ' with \').

The best way is to use selection and selection arguments, so it is done for you:

mContext.getContentResolver().query(Model.CONTENT_URI, null, SELECTION,
                    new String[] { ARGUMENT }, null);

Where SELCTION contains ? signs, eg: name=?

Upvotes: 0

Jamie - Decodefy Ltd
Jamie - Decodefy Ltd

Reputation: 1397

The key is in the error message:

no such column: gmail

you see the query that it's trying to run includes

WHERE bookmarkname=gmail

Your query needs to be modified to indicate that it's a string - in your cursor, change:

BOOKMARK_NAME + "=" + bookmarkName1

for:

BOOKMARK_NAME + "='" + bookmarkName1 + "'"

This will then put the value of "bookmarkName1" in single quote marks.

Selvin's edit: or even better, use parameters(using them will avoid future problems with strings containing '):

db.query(true, BOOKMARKS_TABLE, new String[] {BOOKMARK_ADDRESS},
                BOOKMARK_NAME + "=?", 
                new String[] { bookmarkName1 }, 
null, null, null, null);

Upvotes: 2

Related Questions