如何从SQLite数据库中获取特定行的值?

时间:2014-10-01 12:20:04

标签: android android-sqlite

我有下表并且工作正常:

    public static final String BookID           = "books_ID";           
    public static final String Book             = "books";              
    public static final String Author           = "Authors";            
    private static final String BookAuthor_TABLE    = bookAuthor_Table";
    private static final String[] BookAuthor_AllKeys = new String[] { BookID, Book, Author };   
    private static final String BookAuthor_CREATE   =           
     "CREATE TABLE " + BookAuthor_TABLE + "("+ 
                                    BookID + " INTEGER PRIMARY KEY," + 
                                    Book + " TEXT," + 
                                    Author + " TEXT)";

......

......


    public ArrayList<String> getSpecificColumn(String book) {
        ArrayList<String> AUTHOR_ARRAYLIST;
        AUTHOR_ARRAYLIST = new ArrayList<String>();

        db = getWritableDatabase;       
        String WHERE = Book + "=" + book;
        Cursor cursor = db.query(BookAuthor_TAB, BookAuthor_AllKeys, WHERE, null, null, null, null);

        while(cursor.moveToNext())
        {
            String AUTHOR = cursor.getString(cursor.getColumnIndex(Author));
            AUTHOR_ARRAYLIST.add(AUTHOR);
        }

        return AUTHOR_ARRAYLIST;
    }

MainActivity:

String book = “Jeff Jordan”
AUTHOR_ARRAY = MySQLITE_DATABASE.getSpecificColumn(book);
System.out.println(AUTHOR_ARRAY);

为什么我收到此错误

(1) no such column: Jeff Jordan ?????

我检查了桌子,并且有#34; Jeff Jordan&#34;在作者专栏中。

我做错了什么?

非常感谢

1 个答案:

答案 0 :(得分:4)

字符串文字应该在'single quotes'中,否则它们将被视为列名等标识符。

尽管使用?参数更好:

String WHERE = Book + "=?";
Cursor cursor = db.query(BookAuthor_TAB, BookAuthor_AllKeys,
    WHERE, new String[] { book },
    null, null, null);