表格游戏没有名为“title”的列,编译时出现错误:INSERT INTO game(title, rating, info) VALUES (?, ?, ?);

3

我在尝试向我的数据库插入一个项目时遇到了错误。

下面是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);
    }
}

非常感谢您的帮助。


这个问题出现在模拟器上还是手机上? - kosa
我正在模拟器上运行这个程序。 - Stephen Herring
看起来你的查询没问题(除非我漏掉了什么),你能否从模拟器中完全卸载应用程序,然后再试一次? - kosa
我同意@thinksteep的观点,关于代码和查询似乎没问题。 - StErMi
1个回答

4
您的数据库中有一张名为“game”的表格。但它没有一个名为“title”的列。您可以尝试删除整个数据库并重新创建它。

我认为我将它添加到了GAME_TABLE_CREATE作为KEY_TITLE。这样做有问题吗? - Stephen Herring
卸载应用程序...我敢打赌你是在没有这个列的情况下创建了这个表,然后在代码中添加了它...但模拟器/设备上的数据库没有改变(你没有改变DATABASE_VERSION)(即使你改变它也不会起作用,因为你的onUpgrade方法是空的,通常我们在这里删除旧表并重新创建或者我们可以做ALTER table或其他操作)。 - Selvin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接