Helper Class Android SQLiteOpenHelper SQLiteDatabase

import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteDatabase import android.content.ContentValues
import android.content.Context
import android.database.Cursor class Helper (context: Context?):SQLiteOpenHelper(context,DATABASE_NAME,null,1)
{ override fun onCreate(db: SQLiteDatabase) {
db.execSQL(

"CREATE TABLE " + TABLE_NAME + "("

+ KEY_ID + " INTEGER PRIMARY KEY, "

+ KEY_NOM + " TEXT,"

+ KEY_VILLE + " TEXT,"

+ KEY_LAT + " TEXT, "

+ KEY_LONG + " TEXT )"
)
} override fun onUpgrade(
db: SQLiteDatabase, oldVersion: Int,
newVersion: Int
) {
} fun addCentre(centre: Centre) {
val db = this.writableDatabase
val cv = ContentValues()
cv.put(KEY_NOM, centre.getNom())
cv.put(KEY_VILLE, centre.getVille())
cv.put(KEY_LAT, centre.getLatitude())
cv.put(KEY_LONG, centre.getLongitude())
db.insert(TABLE_NAME, null, cv)
db.close()
} fun fetchAllCenter(): Cursor {
val db = this.readableDatabase
/* if (mCursor != null) {
mCursor.moveToFirst();
}*/return db.query(
TABLE_NAME, arrayOf(
KEY_ID,
KEY_NOM, KEY_VILLE, KEY_LAT, KEY_LONG
),
null, null, null, null, null
)
} fun deleteCenter(id: Int) {
val db = this.writableDatabase
db.delete(TABLE_NAME, KEY_ID + "=?", arrayOf(id.toString()))
} fun updateCentre(id: Int, centre: Centre) {
val db = this.readableDatabase
val cv = ContentValues()
cv.put(KEY_NOM, centre.getNom())
cv.put(KEY_VILLE, centre.getVille())
cv.put(KEY_LAT, centre.getLatitude())
cv.put(KEY_LONG, centre.getLongitude())
db.update(TABLE_NAME, cv, KEY_ID + "=?", arrayOf(id.toString()))
} fun getCentre(id: Int): Centre {
val db = this.readableDatabase
val c = db.query(
TABLE_NAME,
arrayOf(
KEY_ID,
KEY_NOM,
KEY_VILLE,
KEY_LAT,
KEY_LONG
),
KEY_ID + "=?",
arrayOf(id.toString()),
null,
null,
null,
null
)
c?.moveToFirst()
return Centre(
c!!.getInt(0), c.getString(1),
c.getString(2), c.getString(3),
c.getString(4)
)
} companion object {
private const val DATABASE_NAME = "centerManger"
private const val TABLE_NAME = "centre"
private const val KEY_ID = "_id"
private const val KEY_NOM = "nom"
private const val KEY_VILLE = "ville"
private const val KEY_LAT = "latitude"
private const val KEY_LONG = "longitude"
}
}

Commentaires

Posts les plus consultés de ce blog

Premier tutoriel avec Arduino et Proteus ISIS

Login et password ,formulaire d'authentification sous android avec mysql et json

Connexion d'une application Android et une base de données externe Mysql avec web services php Json (opérations CRUD)