2014-04-07 78 views
1

我的數據庫有問題。我已經在網站上舉了一個例子來創建使用數據庫所需的一切。不幸的是,它不想工作。我得到了帶有文本的錯誤消息:表gifs沒有名爲rating的列,而試圖向該表插入對象。我理解這個消息,但我真的不明白爲什麼如果一切都應該正常工作。這是簡單的數據庫,現在應該添加對象。爲什麼在數據庫創建時無法找到該列?Android卡住的數據庫

下面是從MAINACTIVITY代碼:

dataBaseManager.insertGifToDataBase(tmpGif); 

其中tmpGif具有字段:姓名,日期,等級和網址。除了評價其餘的是字符串。

這是我SQLiteHelper和類wchich管理數據庫:

public class SqliteHelper extends SQLiteOpenHelper{ 
public static final String TABLE_NAME = "gifs"; 
    public static final String NAME = "name"; 
    public static final String RATING = "rating"; 
    public static final String DATE = "date"; 
    public static final String URL = "url"; 

    private static final String DATABASE_NAME = "gifs.db"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + "text" + "," + RATING + "int" + "," + URL + "text" + "," + DATE + "text" + ");"; 

public SqliteHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    //Log.i("DATA BASE", getDatabaseName()); 
    } 

@Override 
public void onCreate(SQLiteDatabase database) { 

    database.execSQL(DATABASE_CREATE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 

    database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    onCreate(database); 

} 
} 

回答

2

缺少列名和數據類型之間的空間在正確的結構創建表查詢:

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + "text" + "," + RATING + "int" + "," + URL + "text" + "," + DATE + "text" + ");"; 

應該是:

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING + " int" + "," + URL + " text" + "," + DATE + " text" + ");"; 

所以未正確創建表和列無法找到。

+0

感謝您的註釋。這有助於 – Darek

+0

很高興它幫助,歡迎 – user2450263

1

你需要列的名字和類型之間這裏的空間:

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING + " int" + "," + URL + " text" + "," + DATE + " text" + ");"; 

所以如評級是rating int而不是ratingint

更新SQL後,卸載您的應用程序,以便舊數據庫文件被刪除。

+0

感謝您快速的解答。是的,現在它在你的步驟後起作用。非常感謝 – Darek

1

有必要爲你的DATABASE_CREATE

略有變化,請做如下

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING + " int" + "," + URL + " text" + "," + DATE + " text" + ");";