2011-12-29 24 views
0

我試圖創建一個包含4列(包括_id)的表,並且無論我做什麼,它都會一直創建只有3列(包括_id)的表。使用錯誤的列號創建數據庫

我的表定義如下:

private static final String DATABASE_CREATE = 
     "create table collections (_id integer primary key autoincrement, " 
     + "name text not null, description text not null, image integer not null);"; 

而且我的onCreate:

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

我如何訪問數據:

public View getView(int position, View convertView, ViewGroup parent) { 

     cursor.moveToPosition(position); 

     LinearLayout ll = new LinearLayout(mContext); 
     ll.setOrientation(LinearLayout.VERTICAL); 

     ImageView i = new ImageView(mContext); 


     i.setImageResource(cursor.getInt(3)); //access to the image column 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setLayoutParams(new Gallery.LayoutParams(150, 150)); 

     // The preferred Gallery item background 
     i.setBackgroundResource(mGalleryItemBackground); 


     ll.addView(i); 

     TextView tv = new TextView(ll.getContext()); 

     tv.setTag(mText[position]); 
     tv.setText(cursor.getString(1)); //access to the name column 
     tv.setLayoutParams(new Gallery.LayoutParams(48, 48)); 
     ll.addView(tv); 

     return ll; 
    } 

無論我怎麼努力,它不斷將列圖像從表格中移出...我將很感激我獲得的任何幫助。

+0

你有沒有嘗試命名列'_image'? – 2011-12-29 03:34:12

+0

嗯,我自己也有同樣的問題,請參閱http://stackoverflow.com/questions/8656146/column-not-created-in-sqlite3-table。如果你發現原因,請告訴我。我重寫了我的代碼和列名無數次,似乎沒有任何工作。它就像一個幽靈專欄。 – 2011-12-29 03:37:52

+0

將圖像列名更改爲其他內容並嘗試。 – kosa 2011-12-29 03:42:36

回答

0

你有沒有試過在你的表名和列名中包括反引號?這將做一些轉義爲您提供:

private static final String DATABASE_CREATE = 
    "create table `collections` (`_id` integer primary key autoincrement, " 
    + "`name` text not null, `description` text not null, `image` integer not null);"; 
+0

試過這個解決方案,它沒有工作,得到一個警告消息,該表不存在,而不是。 – Luthien 2011-12-29 04:14:19

0

野生未經證實的猜測:作爲_id可能被忽略或視爲默認_ID列,它可能就不會被計算在內。

試圖讓動態列數:

public View getView(int position, View convertView, ViewGroup parent) { 

    cursor.moveToPosition(position); 

    LinearLayout ll = new LinearLayout(mContext); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    ImageView i = new ImageView(mContext); 


    i.setImageResource(cursor.getInt(cursor.getColumnIndex("image"))); //access to the image column 
    i.setScaleType(ImageView.ScaleType.FIT_XY); 
    i.setLayoutParams(new Gallery.LayoutParams(150, 150)); 

    // The preferred Gallery item background 
    i.setBackgroundResource(mGalleryItemBackground); 


    ll.addView(i); 

    TextView tv = new TextView(ll.getContext()); 

    tv.setTag(mText[position]); 
    tv.setText(cursor.getString(cursor.getColumnIndex("name"))); //access to the name column 
    tv.setLayoutParams(new Gallery.LayoutParams(48, 48)); 
    ll.addView(tv); 

    return ll; 
} 

要休息(題外話):

  1. 你應該閱讀有關適配器回收的觀點
  2. 你也可以嘗試SimpleCursorAdapter類
+0

沒有工作。在這種情況下,我不能使用SimpleCursorAdapter,因爲我使用圖庫來顯示數據。 – Luthien 2011-12-29 20:03:31

+0

但getColumnIndex()爲你工作? – WarrenFaith 2011-12-29 23:22:18

1

這是一個非常愚蠢的事情,我真的很抱歉浪費任何人的時間。我非常感謝大家的幫助。

我沒有正確讀取數據。因此,如果任何人重複我的錯誤,請首先檢查您的獲取方法。