Bu dersimizde veritabanımızın oluşturulması ve güncellenmesi işlemlerini gerçekleştirilen sqlite databasehelper sınıfını oluşturacağız.
Bu derste yazılan kodlar aşağıdaki gibidir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package emrealtunbilek.com.notdefterim.data; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import emrealtunbilek.com.notdefterim.data.NotDefterimContract.KategoriEntry; import emrealtunbilek.com.notdefterim.data.NotDefterimContract.NotlarEntry; public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME="notdefterim.db"; private static final int DATABASE_VERSION=2; private static final String TABLE_KATEGORILER_CREATE= "CREATE TABLE " + KategoriEntry.TABLE_NAME + " ("+ KategoriEntry._ID + " INTEGER PRIMARY KEY, "+ KategoriEntry.COLUMN_KATEGORI + " TEXT)"; private static final String TABLE_NOTLAR_CREATE= "CREATE TABLE " + NotlarEntry.TABLE_NAME + " ("+ NotlarEntry._ID + " INTEGER PRIMARY KEY, "+ NotlarEntry.COLUMN_NOT_ICERIK + " TEXT, " + NotlarEntry.COLUMN_OLUSTURULMA_TARIHI + " TEXT DEFAULT CURRENT_TIMESTAMP, "+ NotlarEntry.COLUMN_BITIS_TARIHI + " TEXT,"+ NotlarEntry.COLUMN_YAPILDI + " INTEGER," + NotlarEntry.COLUMN_KATEGORI_ID + " INTEGER,"+ " FOREIGN KEY ( "+ NotlarEntry.COLUMN_KATEGORI_ID + ")" + " REFERENCES "+ KategoriEntry.TABLE_NAME + "("+ KategoriEntry._ID + ") " + ")"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_KATEGORILER_CREATE); db.execSQL(TABLE_NOTLAR_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + KategoriEntry.TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS "+ NotlarEntry.TABLE_NAME); onCreate(db); } } |