-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.example.jaringobi.db | ||
|
||
import android.provider.BaseColumns | ||
|
||
object Contract { | ||
const val DATABASE_NAME = "jaringobi.db" | ||
const val DATABASE_VERSION = 1 | ||
|
||
object GoalEntry : BaseColumns { | ||
private const val TABLE_NAME = "goals" | ||
private const val COLUMN_YEAR = "year" | ||
private const val COLUMN_MONTH = "month" | ||
private const val COLUMN_AMOUNT = "amount" | ||
private const val COLUMN_CURRENT_SPENT = "current_spent" | ||
|
||
const val CREATE_TABLE = | ||
"CREATE TABLE $TABLE_NAME" + | ||
"(" + | ||
"${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + | ||
"$COLUMN_YEAR INTEGER NOT NULL, " + | ||
"$COLUMN_MONTH INTEGER NOT NULL, " + | ||
"$COLUMN_AMOUNT INTEGER NOT NULL, " + | ||
"$COLUMN_CURRENT_SPENT INTEGER NOT NULL DEFAULT 0" + | ||
")" | ||
|
||
const val DROP_TABLE = "DROP TABLE IF EXISTS $TABLE_NAME" | ||
} | ||
|
||
object ExpenseEntry : BaseColumns { | ||
private const val TABLE_NAME = "expenses" | ||
private const val COLUMN_PLACE = "place" | ||
private const val COLUMN_AMOUNT = "amount" | ||
private const val COLUMN_DATE = "date" | ||
|
||
const val CREATE_TABLE = | ||
"CREATE TABLE $TABLE_NAME" + | ||
"(" + | ||
"${BaseColumns._ID} INTEGER PRIMARY KEY AUTOINCREMENT," + | ||
"$COLUMN_PLACE TEXT NOT NULL," + | ||
"$COLUMN_AMOUNT INTEGER NOT NULL," + | ||
"$COLUMN_DATE TEXT NOT NULL" + | ||
")" | ||
|
||
const val DROP_TABLE = "DROP TABLE IF EXISTS $TABLE_NAME" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.example.jaringobi.db | ||
|
||
import android.content.Context | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.database.sqlite.SQLiteOpenHelper | ||
import com.example.jaringobi.db.Contract.ExpenseEntry | ||
import com.example.jaringobi.db.Contract.GoalEntry | ||
|
||
class DBHelper(context: Context) : SQLiteOpenHelper( | ||
context, | ||
Contract.DATABASE_NAME, | ||
null, | ||
Contract.DATABASE_VERSION, | ||
) { | ||
override fun onCreate(db: SQLiteDatabase?) { | ||
Thread { | ||
db?.execSQL(ExpenseEntry.CREATE_TABLE) | ||
db?.execSQL(GoalEntry.CREATE_TABLE) | ||
}.start() | ||
} | ||
|
||
override fun onUpgrade( | ||
db: SQLiteDatabase?, | ||
oldVersion: Int, | ||
newVersion: Int, | ||
) { | ||
Thread { | ||
db?.execSQL(ExpenseEntry.DROP_TABLE) | ||
db?.execSQL(GoalEntry.DROP_TABLE) | ||
}.start() | ||
|
||
onCreate(db) | ||
} | ||
} |