2

I'm trying to select a specific row from a database based on a string in a textview. The error log when I run it appears to say that it's looking for a column, but it should be looking for a row. The error log says:

    --I/Database(279): sqlite returned: error code = 1, msg = no such column: Book
--D/AndroidRuntime(279): Shutting down VM
--W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
--E/AndroidRuntime(279): FATAL EXCEPTION: main
--E/AndroidRuntime(279): android.database.sqlite.SQLiteException: no such column: Book: ,   while compiling: SELECT book, background FROM bookbackgrounds WHERE book=Book

Here's the code where it gets the string from the textview, then runs a method that is supposed to return a filename in the form of a text string, then display the returned string in a second textview:

                String bkString = showBooktextview.getText().toString();
            bga.open();
            String newBKstring = bga.getBGforBook(bkString);
            bga.close();                
            showDBBooktextview.setText(newBKstring);

and here's the method that the error says there's no such column with the name of that book. (it's supposed to be looking for the ROW that contains that book name):

        public String getBGforBook(String bkString) {
    String[] thecolumns = new String[] { KEY_BOOK, KEY_BACKGROUND };
    Cursor cursor = db.query(DB_TABLE, thecolumns, KEY_BOOK + "=" + bkString, null, null,       null, null);
    String result = "";

    if (cursor != null){ 
        cursor.moveToFirst();
    result = result
    + cursor.getString(1);
    }
    return null;
    }

so is the method written incorrectly? If so, how to write it correctly so that it looks for a row that contains the bookname string?

1 Answer 1

7

I think you are missing single quotes in the condition expression:

KEY_BOOK + "=" + bkString

should be

KEY_BOOK + "='" + bkString + "'"

Since the string bkString is not quoted, SQLite tries to interpret it as an identifier of a column name.

Sign up to request clarification or add additional context in comments.

3 Comments

adding the single quotes appears to make Eclipse completely ignore the method, but it doesn't crash the app.
@kirktoon1882 What do you mean "ignore the method"? Is it not returning what you expected? This is probably because you are returning null either way; you should be returning result.
SWEET! Your fix worked! I'm not surprised though, you've got a 50.4k rep, obviously not a rookie like me! Ok, so you were right in your comment, I was returning null, not "result". So it looked like the method wasn't doing anything, but it was working, and it was returning nothing. Thanks again! you RULE!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.