如何将列添加到android sql?

时间:2018-01-13 10:07:57

标签: android android-sqlite

我尝试在存在8列的现有数据库中添加三列。

我找到了一些解决方法,在onUpgrade覆盖方法上键入代码并尝试将列添加到我现有的数据库中。不幸的是我得到了这样的错误java.lang.IllegalStateException: Couldn't read row 0, col 8 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

以下是关于onCreatonUpgrade的代码:

public class DatabaseHandler extends SQLiteOpenHelper {

    private String TAG = DatabaseHandler.class.getCanonicalName();
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "contacts";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_DATE = "date";
    private static final String KEY_BMORNING = "bMorning";
    private static final String KEY_AMORNING = "aMorning";
    private static final String KEY_BNOON = "bNoon";
    private static final String KEY_ANOON = "aNoon";
    private static final String KEY_BNIGHT = "bNight";
    private static final String KEY_ANIGHT = "aNight";
    private static final String KEY_BEXERCISE = "bExercise";
    private static final String KEY_AEXERCISE = "aExercise";
    private static final String KEY_COMMENT = "comment";

    String date, value;


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

    //Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("table is here");
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
                + KEY_BMORNING + " TEXT," + KEY_AMORNING + " TEXT,"
                + KEY_BNOON + " TEXT," + KEY_ANOON + " TEXT,"
                + KEY_BNIGHT + " TEXT," + KEY_ANIGHT + " TEXT" +
                ")";
        db.execSQL(CREATE_CONTACTS_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Drop order table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        Log.d(TAG, "DB Version =>" + db.getVersion()); // Can't see it on logcat
        if (oldVersion < 2) {
            db.execSQL("ALTER TABLE " + TABLE_CONTACTS + " ADD COLUMN " + KEY_BEXERCISE + " TEXT;");
            db.execSQL("ALTER TABLE " + TABLE_CONTACTS + " ADD COLUMN " + KEY_AEXERCISE + " TEXT;");
            db.execSQL("ALTER TABLE " + TABLE_CONTACTS + " ADD COLUMN " + KEY_COMMENT + " TEXT;");
        } if (oldVersion < 3) {
            //do nothing temporarily
        }
        //Create table again
        onCreate(db);
    }
}

这是关于启动数据库代码的MainActivity:

DatabaseHandler db = new DatabaseHandler(getActivity());
Log.d("Insert: ", "Inserting...");
Log.d("Reading: ", "Reading all contact...");
List<Contact> contacts = db.getAllContacts(); // this code cause the error happen

我猜可能是覆盖方法onUpgrade没有触发器的原因,因为我无法看到我的日志数据库版本。

我真的无法弄清楚。

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

将DATABASE_VERSION增加1并再次运行