added crud
This commit is contained in:
parent
53fd0880a2
commit
df4ba4c1b6
@ -0,0 +1,147 @@
|
|||||||
|
package com.wyattjmiller.classscheduleapp
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.content.Context
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
import android.os.Build
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
|
class CourseDatabase private constructor(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, VERSION) {
|
||||||
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(
|
||||||
|
"create table " + CourseTable.TABLE + " (" +
|
||||||
|
CourseTable.COL_ID + " primary key autoincrement, " +
|
||||||
|
CourseTable.COL_NAME + " int, " +
|
||||||
|
CourseTable.COL_DESCRIPTION + " string, " +
|
||||||
|
CourseTable.COL_LOCATION + " string, " +
|
||||||
|
CourseTable.COL_DAYOFWEEK + " string, " +
|
||||||
|
CourseTable.COL_TIME + " string, " +
|
||||||
|
CourseTable.COL_INSTRUCTOR + " string, " +
|
||||||
|
CourseTable.COL_UPDATE_TIME + " int)"
|
||||||
|
)
|
||||||
|
|
||||||
|
val course = Course()
|
||||||
|
val values = ContentValues()
|
||||||
|
values.put(CourseTable.COL_NAME, course.name)
|
||||||
|
db.insert(CourseTable.TABLE, null, values)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||||
|
db.execSQL("drop table if exists " + CourseTable.TABLE)
|
||||||
|
onCreate(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOpen(db: SQLiteDatabase) {
|
||||||
|
super.onOpen(db)
|
||||||
|
|
||||||
|
if (!db.isReadOnly) {
|
||||||
|
// Enable foreign key constraints
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
|
||||||
|
db.execSQL("pragma foreign_keys = on;")
|
||||||
|
} else {
|
||||||
|
db.setForeignKeyConstraintsEnabled(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCourses(course: String): List<Course> {
|
||||||
|
val courses: MutableList<Course> = ArrayList<Course>()
|
||||||
|
val db = this.readableDatabase
|
||||||
|
val sql = ("select * from " + CourseTable.TABLE)
|
||||||
|
val cursor = db.rawQuery(sql, arrayOf(course))
|
||||||
|
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
val course = Course()
|
||||||
|
course.id = cursor.getLong(0)
|
||||||
|
course.name = cursor.getString(1)
|
||||||
|
course.description = cursor.getString(2)
|
||||||
|
course.location = cursor.getString(3)
|
||||||
|
course.dayOfWeek = cursor.getString(4)
|
||||||
|
course.time = cursor.getString(5)
|
||||||
|
course.instructor = cursor.getString(6)
|
||||||
|
courses.add(course)
|
||||||
|
} while (cursor.moveToNext())
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
return courses
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCourse(questionId: Long): Course? {
|
||||||
|
var course: Course? = null
|
||||||
|
val db = this.readableDatabase
|
||||||
|
val sql = ("select * from " + CourseTable.TABLE +
|
||||||
|
" where " + CourseTable.COL_ID + " = ?")
|
||||||
|
val cursor = db.rawQuery(sql, arrayOf(java.lang.Float.toString(questionId.toFloat())))
|
||||||
|
|
||||||
|
if (cursor.moveToFirst()) {
|
||||||
|
course?.id = cursor.getLong(0)
|
||||||
|
course?.name = cursor.getString(1)
|
||||||
|
course?.description = cursor.getString(2)
|
||||||
|
course?.location = cursor.getString(3)
|
||||||
|
course?.dayOfWeek = cursor.getString(4)
|
||||||
|
course?.time = cursor.getString(5)
|
||||||
|
course?.instructor = cursor.getString(6)
|
||||||
|
}
|
||||||
|
|
||||||
|
return course
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addCourse(course: Course) {
|
||||||
|
val db = writableDatabase
|
||||||
|
val values = ContentValues()
|
||||||
|
|
||||||
|
values.put(CourseTable.COL_NAME, course.name)
|
||||||
|
values.put(CourseTable.COL_DESCRIPTION, course.description)
|
||||||
|
values.put(CourseTable.COL_LOCATION, course.location)
|
||||||
|
values.put(CourseTable.COL_DAYOFWEEK, course.dayOfWeek)
|
||||||
|
values.put(CourseTable.COL_TIME, course.time)
|
||||||
|
values.put(CourseTable.COL_INSTRUCTOR, course.instructor)
|
||||||
|
|
||||||
|
val courseId = db.insert(CourseTable.TABLE, null, values)
|
||||||
|
|
||||||
|
course.id = courseId
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateCourse(course: Course) {
|
||||||
|
val db = writableDatabase
|
||||||
|
val values = ContentValues()
|
||||||
|
|
||||||
|
values.put(CourseTable.COL_ID, course.id)
|
||||||
|
values.put(CourseTable.COL_NAME, course.name)
|
||||||
|
values.put(CourseTable.COL_DESCRIPTION, course.description)
|
||||||
|
values.put(CourseTable.COL_LOCATION, course.location)
|
||||||
|
values.put(CourseTable.COL_DAYOFWEEK, course.dayOfWeek)
|
||||||
|
values.put(CourseTable.COL_TIME, course.time)
|
||||||
|
values.put(CourseTable.COL_INSTRUCTOR, course.instructor)
|
||||||
|
|
||||||
|
db.update(
|
||||||
|
CourseTable.TABLE, values,
|
||||||
|
CourseTable.COL_ID + " = " + course.id, null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteCourse(courseId: Long) {
|
||||||
|
val db = writableDatabase
|
||||||
|
db.delete(
|
||||||
|
CourseTable.TABLE,
|
||||||
|
CourseTable.COL_ID + " = " + courseId, null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val VERSION = 1
|
||||||
|
private val DATABASE_NAME = "study.db"
|
||||||
|
private var mStudyDb: CourseDatabase? = null
|
||||||
|
|
||||||
|
fun getInstance(context: Context): CourseDatabase? {
|
||||||
|
if (mStudyDb == null) {
|
||||||
|
mStudyDb = CourseDatabase(context)
|
||||||
|
}
|
||||||
|
return mStudyDb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user