2012-01-09 23 views
3

我在嘗試向我的數據庫中插入一個項目時出現錯誤。桌面遊戲沒有列名稱標題:,而編譯:INSERT INTO遊戲(標題,評分,信息)VALUES(?,?,?);

這是logcat中寫道:

android.database.sqlite.SQLiteException table game has no column named title: , while compiling: INSERT INTO game(title, rating, info) VALUES (?, ?, ?); 

這是我的數據庫管理器:

package com.herring.android.finalproject; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class GameDataBaseManager { 

    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 
    private static final String DATABASE_NAME = "data"; 
    private static final int DATABASE_VERSION = 1; 
    private final Context mCtx; 
    private static final String GAME_TABLE_NAME = "game"; 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_TITLE = "title"; 
    public static final String KEY_BODY = "info"; 
    public static final String KEY_RATING = "rating"; 
    private static final String GAME_TABLE_CREATE = "create table " + GAME_TABLE_NAME + " (" + 
      KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      KEY_TITLE + " TEXT NOT NULL, " + 
      KEY_BODY + " TEXT NOT NULL, " + 
      KEY_RATING + " NUM NOT NULL);"; 


    public GameDataBaseManager(Context ctx) 
    { 
     this.mCtx = ctx; 
    } 


    public long addRow(String rowStringOne, String rowStringTwo, float rating) 
    { 
     ContentValues values = new ContentValues(); 
     values.put(KEY_TITLE, rowStringOne); 
     values.put(KEY_BODY, rowStringTwo); 
     values.put(KEY_RATING, rating); 
     return mDb.insert(GAME_TABLE_NAME, null, values); 
    } 
    public void deleteRow(long rowID) 
    { 
     try 
     { 
      mDb.delete(GAME_TABLE_NAME, KEY_ROWID + " = " + rowID, null); 
     } 
     catch(Exception e) 
     { 
      Log.e("DB ERROR", e.toString()); 
      e.printStackTrace(); 
     } 
    } 


    public GameDataBaseManager open() throws android.database.SQLException 
    { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() 
    { 
     mDbHelper.close(); 
    } 

    public Cursor getAllGames() 
    { 
     return mDb.query(GAME_TABLE_NAME, null, null, 
       null, null, null, null); 
    } 

    public Cursor fetchGame(long rowId) throws SQLException 
    { 
     Cursor mCursor = mDb.query(true, GAME_TABLE_NAME, new String[]{KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_RATING}, 
       KEY_ROWID + "=" + rowId, null, null, null, null, null); 
     if(mCursor != null) 
     { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    public boolean updateDb(long rowId, String title, String body, String rating) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_TITLE, title); 
     args.put(KEY_BODY, body); 
     args.put(KEY_RATING, rating); 

     return mDb.update(GAME_TABLE_NAME, args, KEY_ROWID + " = " + rowId, null) > 0; 
    } 

    public class DatabaseHelper extends SQLiteOpenHelper { 
     public DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

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

     } 

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

     } 
    } 
} 

這裏就是我試圖插入項目的活動:

package com.herring.android.finalproject; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class TopRatedActivity extends Activity { 
    private Cursor gamesCursor; 
    private ListView lv; 
    private GameDataBaseManager mDbHelper; 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     mDbHelper = new GameDataBaseManager(this); 
     mDbHelper.open(); 
     mDbHelper.addRow("Game", "Info", 0); 
     setContentView(R.layout.toprated); 
     fillData(); 

    } 
    private void fillData() 
    { 
     gamesCursor = mDbHelper.getAllGames(); 
     startManagingCursor(gamesCursor); 
     String[] from = new String[]{GameDataBaseManager.KEY_TITLE}; 
     int[] to = new int[]{R.id.text1}; 
     SimpleCursorAdapter games = new SimpleCursorAdapter(this, R.layout.toprateditem, gamesCursor, from, to); 
     lv.setAdapter(games); 
    } 
} 

任何幫助將不勝感激。

+0

模擬器(或)在手機上是否存在此問題? – kosa 2012-01-09 14:54:15

+0

我在模擬器上運行它。 – 2012-01-09 14:55:49

+0

看來你的查詢看起來不錯(除非我錯過了某些東西),你能完全卸載從模擬器的應用程序,然後再試一次嗎? – kosa 2012-01-09 14:57:30

回答

4

你在你的數據庫中有一個名爲「遊戲」的表。但它沒有名爲「title」的列。你可以嘗試刪除整個數據庫並重新創建它。

+0

我以爲我將它作爲KEY_TITLE添加到GAME_TABLE_CREATE中。那是錯的嗎? – 2012-01-09 14:53:32

+0

卸載應用程序...我敢打賭,你已經創建了這個沒有此列的表,然後將它添加到代碼中......但模擬器/設備上的數據庫沒有改變(你沒有改變DATABASE_VERSION)(好吧,即使你改變它不會工作因爲你的onUpgrade方法是空的,通常我們刪除舊錶並在這裏重新創建,或者我們可以做ALTER表或其他) – Selvin 2012-01-09 14:56:10