Created
August 13, 2018 16:07
-
-
Save abdul-rehman-2050/892582c4b5777b0d1bcaf276456dbf02 to your computer and use it in GitHub Desktop.
SQLite anko example
This file contains hidden or 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
package com.quote.my.mark.mmq.database | |
import android.content.Context | |
import android.database.sqlite.SQLiteDatabase | |
import org.jetbrains.anko.db.* | |
class QuoteDBHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "MMQDatabase", null, 1) { | |
companion object { | |
private var instance: QuoteDBHelper? = null | |
@Synchronized | |
fun getInstance(ctx: Context): QuoteDBHelper { | |
if (instance == null) { | |
instance = QuoteDBHelper(ctx.getApplicationContext()) | |
} | |
return instance!! | |
} | |
} | |
override fun onCreate(db: SQLiteDatabase) { | |
// Here you create tables | |
db.createTable("Quotes", true, | |
"id" to INTEGER + PRIMARY_KEY + UNIQUE + AUTOINCREMENT, | |
"quote_text" to TEXT, | |
"author" to TEXT, | |
"source" to TEXT, | |
"creation_date" to TEXT, | |
"is_favourite" to INTEGER | |
) | |
} | |
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { | |
// Here you can upgrade tables, as usual | |
db.dropTable("Quotes", true) | |
} | |
} | |
// Access property for Context | |
val Context.database: QuoteDBHelper | |
get() = QuoteDBHelper.getInstance(applicationContext) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment