Error inserting data to SQLite database

早过忘川 提交于 2020-01-24 21:38:05

问题


I have a method to create a Farm object and insert it into the database, the method is as follows:

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 is the name of the table ("farms") and COLUMN_NAME is the column name ("name"). The error I'm getting is:

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)

Edit: whole class:

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:


your code for creating the database doesn't seem inaccurate for what you're trying to achieve. whenever i have problems like this they are usually the result of working with an old/outdated version of my database schema that doesn't match my current code. i resolve it by:

  • if i'm running on the emulator, i'll delete the database via DDMS (i usually pull down a copy and inspect it to verify my assumptions as well), or
  • if i'm running on a physical device, i'll uninstall the app so as to rebuild the database from scratch (invoking the "Clear data" option from the Apps browser might serve just as well in this case... i've never tried)

hope that helps.



来源:https://stackoverflow.com/questions/22729145/error-inserting-data-to-sqlite-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!