2011-07-03 35 views
0

1月7日至3日:52:08.037: ERROR/AndroidRuntime(627): 了java.lang.RuntimeException:無法 開始活動 ComponentInfo {com.fttech.books /com.fttech.books.viewBooks}: android.database.sqlite.SQLiteException: 沒有這樣的列:作者:,而 編譯:選擇預訂,作者,ISBN 評級從收集我不斷收到錯誤的SQLException這個

CODE:

public class booksDbAdapter { 


private static final String DATABASE_NAME = " data"; 
private static final String DATABASE_TABLE = "collection"; 
private static final int DATABASE_VERSION = 1; 

public static final String KEY_BOOK = "book"; 
public static final String KEY_AUTHOR = "author"; 
public static final String KEY_ISBN = "isbn"; 
public static final String KEY_RATING = "rating"; 
public static final String KEY_ROWID = "_id"; 

private DatabaseHelper mDbHelper; 
private SQLiteDatabase mDb; 


private static final String DATABASE_CREATE = 
     " create table " + DATABASE_TABLE + " (" 
     + KEY_ROWID + " integer primary key autoincrement, " 
     + KEY_AUTHOR + " text not null, " 
     + KEY_BOOK + " text not null, " 
     + KEY_ISBN + " text not null, " 
     + KEY_RATING + " text not null);"; 

private final Context mCtx; 


public booksDbAdapter (Context ctx){ 
    this.mCtx = ctx; 



     } 

     private static class DatabaseHelper extends SQLiteOpenHelper{ 
     DatabaseHelper(Context context){ 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 


     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     db.execSQL(DATABASE_CREATE); 


     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
    } 
} 
     public booksDbAdapter open() throws SQLException{ 

      mDbHelper = new DatabaseHelper(mCtx); 
      mDb = mDbHelper.getWritableDatabase(); 
      return this; 
     } 
     public void close(){ 
      mDbHelper.close(); 
     } 

     public long createBook(String book, String author, String isbn, String rating){ 
      ContentValues initialValues = new ContentValues(); 
      initialValues.put(KEY_BOOK, book); 
      initialValues.put(KEY_AUTHOR, author); 
      initialValues.put(KEY_ISBN, isbn); 
      initialValues.put(KEY_RATING, rating); 

      return mDb.insert(DATABASE_TABLE, null, initialValues); 

     } 
     public boolean deleteBook(long rowId){ 
      return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 

     } 
     public Cursor fetchAllBooks(){   
      return mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR, KEY_ISBN, KEY_RATING}, null, null, null, null, null); 
     } 
     public Cursor fetchBook(long rowId) throws SQLException{ 
      Cursor mCursor = 
      mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR, KEY_ISBN, KEY_RATING}, KEY_ROWID + "=" + 
         rowId, null, null, null, null); 

      if(mCursor != null){ 
       mCursor.moveToFirst(); 
      } 
      return mCursor; 

     } 
     public boolean updateBook(long rowId, String book, String author, String isbn, String rating){ 
      ContentValues args = new ContentValues(); 
      args.put(KEY_BOOK, book); 
      args.put(KEY_AUTHOR, author); 
      args.put(KEY_ISBN, isbn); 
      args.put(KEY_RATING, rating); 
      return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)> 0; 


     } 

} 
+2

「沒有這樣的列:作者」 - 是很具體 –

+0

我知道!我只是無法找到我在哪裏搞亂!我現在一直在尋找一個小時..你看到它了嗎? –

+0

我告訴它創建列,你可以在代碼中看到...所以我沒有得到什麼問題。 –

回答

0

我建議你通過命令行運行.dump sqlite數據庫並檢查它。然後通過命令行運行查詢。如果一切正常,尋找錯別字。找出哪個方法有錯誤的查詢也很有幫助,這樣可以縮小問題的範圍。

0

你的表是否真的有專欄作者?我假設你已經添加了該列通過db.execSQL(DATABASE_CREATE);

您的問題創建表後,你沒有內onUpgrade()任何代碼,所以當你添加一個新列的不添加,因爲數據庫中創建會導致錯誤類似「數據庫已存在」

您需要將代碼添加到onUpgrade(),它將至少刪除所有表然後重新創建。或更好地運行一個更改表等...

然後你需要增加int DATABASE_VERSION所以onUpgrade()將運行。最後你的新列「作者」將被添加到表..

當然,你也可以檢查是否由經command line

希望幫助

這裏是連接到你的數據庫中存在的列我使用的一些代碼可能有助於創建/升級。很明顯,我的表創建常量是特定於我...

編輯:...你也沒有嘗試/抓住你的表創建。我相信,如果添加此,你會發現在表中創建命令沒有運行其獲得「表已經存在,等等..」

... 
DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    try { 
     db.execSQL(CREATE_TABLE_SITE); 
     db.execSQL(CREATE_TABLE_SITE_USER); 
     db.execSQL(CREATE_TABLE_ARTICLE); 
     db.execSQL(CREATE_TABLE_USER); 
    } catch (Exception e) { 
     Log.e("dbAdapter", e.getMessage().toString()); 
    } 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
      + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS user"); 
    db.execSQL("DROP TABLE IF EXISTS site"); 
    db.execSQL("DROP TABLE IF EXISTS site_user"); 
    db.execSQL("DROP TABLE IF EXISTS article"); 
    onCreate(db); 
} 
... 
相關問題