2015年11月5日 星期四

Android-GreenDAO 的基本使用方式 可當小型 資料庫 - 3

如果有需要再新增欄位怎麼辦?
資料庫重建?

如果真的有需要新增欄位
可以直接在 Class 下新增
但要注意不要由遺漏的
沿用上一篇創立的表格 : Note
當創立完後
會產生以下兩個檔案
我們會修改此檔案











比如要在新增有關日期的欄位( 紅字代表新增 )

1 . Note.java ( 4項須更新 )
package de.greenrobot.daoexample;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. 
/**
 * Entity mapped to table NOTE.
 */
public class Note {

    private Long id;
    /** Not-null value. */
    private String note;
    private int count;
    /** Not-null value. */
    private java.util.Date date;
    /** Not-null value. */
    private java.util.Date Hee;

    public Note() {
    }

    public Note(Long id) {
        this.id = id;
    }

    public Note(Long id, String note, int count, java.util.Date date, java.util.Date Hee) {
        this.id = id;
        this.note = note;
        this.count = count;
        this.date = date;
        this.Hee = Hee;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /** Not-null value. */
    public String getNote() {
        return note;
    }

    /** Not-null value; ensure this value is available before it is saved to the database. */
    public void setNote(String note) {
        this.note = note;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    /** Not-null value. */
    public java.util.Date getDate() {
        return date;
    }

    /** Not-null value; ensure this value is available before it is saved to the database. */
    public void setDate(java.util.Date date) {
        this.date = date;
    }

    /** Not-null value. */
    public java.util.Date getHee() {
        return Hee;
    }

    /** Not-null value; ensure this value is available before it is saved to the database. */
    public void setHee(java.util.Date Hee) {
        this.Hee = Hee;
    }

}

2 . NoteDao.java( 5項需更新 )

package de.greenrobot.daoexample;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

import de.greenrobot.daoexample.Note;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table NOTE.
*/
public class NoteDao extends AbstractDao<Note, Long> {

    public static final String TABLENAME = "NOTE";

    /**
     * Properties of entity Note.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Note = new Property(1, String.class, "note", false, "NOTE");
        public final static Property Count = new Property(2, int.class, "count", false, "COUNT");
        public final static Property Date = new Property(3, java.util.Date.class, "date", false, "DATE");
        public final static Property Hee = new Property(4, java.util.Date.class, "Hee", false, "HEE");
    };


    public NoteDao(DaoConfig config) {
        super(config);
    }
    
    public NoteDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'NOTE' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'NOTE' TEXT NOT NULL ," + // 1: note
                "'COUNT' INTEGER NOT NULL ," + // 2: count
                "'DATE' INTEGER NOT NULL ," + // 3: date
                "'HEE' INTEGER NOT NULL );"); // 4: Hee
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'NOTE'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Note entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getNote());
        stmt.bindLong(3, entity.getCount());
        stmt.bindLong(4, entity.getDate().getTime());
        stmt.bindLong(5, entity.getHee().getTime());
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Note readEntity(Cursor cursor, int offset) {
        Note entity = new Note( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // note
            cursor.getInt(offset + 2), // count
            new java.util.Date(cursor.getLong(offset + 3)), // date
            new java.util.Date(cursor.getLong(offset + 4)) // Hee
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Note entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setNote(cursor.getString(offset + 1));
        entity.setCount(cursor.getInt(offset + 2));
        entity.setDate(new java.util.Date(cursor.getLong(offset + 3)));
        entity.setHee(new java.util.Date(cursor.getLong(offset + 4)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Note entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Note entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
}

歡迎轉載,請註明出處。

沒有留言:

張貼留言