android - table game 在编译 : , rating, info) VALUES (?, ?, ?) 时没有名为 title : INSERT INTO game(title, 的列;

标签 android

我在尝试将项目插入我的数据库时遇到错误。

这是 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) {

        }
    }
}

这是我尝试插入项目的 Activity :

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);
    }
}

如有任何帮助,我们将不胜感激。

最佳答案

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

关于android - table game 在编译 : , rating, info) VALUES (?, ?, ?) 时没有名为 title : INSERT INTO game(title, 的列;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790119/

相关文章:

java - Marshmallow 上的 getSerial() 方法

java - 菜单页面上的按钮只能按特定顺序工作。 (Android Studio - Java)

android - 如何继承android xml中的字段?

android - 使 Android Activity 看起来像对话框

android - LinearLayout 中的 TextView 不显示文本部分但显示图像正常

android - 出现错误 “Execution failed for task ':app:compileDebugJavaWithJavac'”

android - 关于 PocketSphinxAndroidDemo

android - Graphhopper 替代路线 Android

android - 即使应用程序已关闭,如何每天在特定时间显示通知?

android - 何时使用 MutableLiveData 和 LiveData