将数据插入SQLite数据库时出错

时间:2014-03-29 09:12:34

标签: android sqlite

我有一个创建Farm对象并将其插入数据库的方法,方法如下:

public Farm createFarm(String name) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FavegaOpenHelper.COLUMN_NAME, name);
        long insertId = database.insert(FavegaOpenHelper.TABLE_FARMS, null, values);
        Cursor cursor = database.query(FavegaOpenHelper.TABLE_FARMS,
                new String[] {COLUMN_ID, COLUMN_NAME}, FavegaOpenHelper.COLUMN_ID +
                        " = " + insertId, null, null, null, null);
        cursor.moveToFirst();
        Farm newFarm = cursorToFarm(cursor);
        cursor.close();
        database.close();
        return newFarm;
    }

TABLE_FARMS是表的名称(" farm"),COLUMN_NAME是列名(" name")。我得到的错误是:

android.database.sqlite.SQLiteException: table farms has no column named farms (code 1): , while compiling: INSERT INTO farms(farms) VALUES (?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
            at com.favega.favega.FavegaOpenHelper.createFarm(FavegaOpenHelper.java:58)
            at com.favega.favega.NavigationDrawerFragment$2$2.onClick(NavigationDrawerFragment.java:151)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)

编辑:全班:

public class FavegaOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "FAVEGA";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_FARMS = "farms";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "name";

    private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_FARMS +
            " (" + COLUMN_ID + " integer primary key autoincrement," + COLUMN_NAME +
            " text not null);";

    public FavegaOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch(oldVersion) {
            case 1:
                switch (newVersion) {
                    default:
                        break;
                }
            default:
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_FARMS);
                onCreate(db);
        }
    }

    public Cursor getFarms() {
        SQLiteDatabase database = this.getWritableDatabase();
        return database.query(FavegaOpenHelper.TABLE_FARMS, new String[] {COLUMN_ID, COLUMN_NAME},
                null, null, null, null, null);
    }

    public Farm createFarm(String name) {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FavegaOpenHelper.COLUMN_NAME, name);
        long insertId = database.insert(FavegaOpenHelper.TABLE_FARMS, null, values);
        Cursor cursor = database.query(FavegaOpenHelper.TABLE_FARMS,
                new String[] {COLUMN_ID, COLUMN_NAME}, FavegaOpenHelper.COLUMN_ID +
                        " = " + insertId, null, null, null, null);
        cursor.moveToFirst();
        Farm newFarm = cursorToFarm(cursor);
        cursor.close();
        database.close();
        return newFarm;
    }

    private Farm cursorToFarm(Cursor cursor) {
        Farm farm = new Farm();
        farm.setId(cursor.getLong(cursor.getColumnIndexOrThrow("_id")));
        farm.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
        return farm;
    }
}

1 个答案:

答案 0 :(得分:0)

用于创建数据库的代码对于您要实现的目标而言似乎并不准确。每当我遇到这样的问题时,通常都是使用与我当前代码不匹配的旧/过时版本的数据库模式。我解决它:

  • 如果我在模拟器上运行,我将通过DDMS删除数据库(我通常会下载副本并检查它以验证我的假设),或者
  • 如果我在物理设备上运行,我将卸载该应用程序以便从头开始重建数据库(在这种情况下,从Apps浏览器调用“清除数据”选项可能同样适用...我从未尝试过)
希望有所帮助。

相关问题