From df4ba4c1b62506fa484e8d0ca93f0cb20dfdca24 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Thu, 5 Nov 2020 00:43:32 -0500 Subject: [PATCH] added crud --- .../classscheduleapp/CourseDatabase.kt | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt diff --git a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt new file mode 100644 index 0000000..635d113 --- /dev/null +++ b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt @@ -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 { + val courses: MutableList = ArrayList() + 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 + } + } +} \ No newline at end of file