20.11.2016 Views

ANDROID APP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ฐานข้อมูล SQLite<br />

235<br />

public MyDBhelper(Context context, String name, CursorFactory factory,<br />

int version) {<br />

super(context, name, factory, version);<br />

}<br />

@Override<br />

public void onCreate(SQLiteDatabase db) {<br />

Log.v("MyDBhelper onCreate","Creating all the tables");<br />

try {<br />

db.execSQL(CREATE_TABLE);<br />

} catch(SQLiteException ex) {<br />

Log.v("Create table exception", ex.getMessage());<br />

}<br />

}<br />

}<br />

@Override<br />

public void onUpgrade(SQLiteDatabase db, int oldVersion,<br />

int newVersion) {<br />

Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion<br />

+" to "+newVersion<br />

+", which will destroy all old data");<br />

db.execSQL("drop table if exists "+Constants.TABLE_NAME);<br />

onCreate(db);<br />

}<br />

ไฟล์ที่ 3 ของแพ็คเกจ com.cookbook.data คือคลาส Constants ตามที่แสดงในชุดคำสั่งที่<br />

9.11 คลาส Constants นี้จะใช้เก็บค่าคงที่ต่างๆ ที่ใช้ใน MyDB และ MyDBhelper<br />

ชุดคำสั่งที่ 9.11 src/com/cookbook/data/Constants.java<br />

package com.cookbook.data;<br />

public class Constants {<br />

public static final String DATABASE_NAME="datastorage";<br />

public static final int DATABASE_VERSION=1;<br />

public static final String TABLE_NAME="diaries";<br />

public static final String TITLE_NAME="title";<br />

public static final String CONTENT_NAME="content";<br />

public static final String DATE_NAME="recorddate";<br />

public static final String KEY_ID="_id";<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!