2014-10-01 117 views
1

我有以下表和它的正常工作:如何從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 ????? 

我檢查表,並有是「傑夫·喬丹」的專欄作者。

我做錯了什麼?

非常感謝

回答

4

字符串文本應該在'single quotes'否則他們被當作標識,如列名。

它甚至更好,使用?參數雖然:

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

耶穌基督,我快要哭了。我花了很多時間在網上搜索並進行故障排除,但從未奏效。你真的幫了我很多。非常感謝!對此,我真的非常感激 – User2014 2014-10-01 12:57:06

相關問題