I have a SqlitieDatebase in android studio and for some reason, the database is not creating. I really like it if someone can help me with this issue.
I am not getting an error t all for the database.
Thank you for your help
This is my code for just starting the datebase
class MainActivity : AppCompatActivity() {
val eventdata = null
val dbHelper = EventDatabasek(this)
val STORAGE_LOCAL =101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
applicationContext
checkPer(WRITE_EXTERNAL_STORAGE, "storage", STORAGE_LOCAL)
val eventDatabasek = EventDatabasek(this)
val calen = findViewById<Button>(R.id.btnCal)
val curr = findViewById<Button>(R.id.btnCurrApp)
val newpla = findViewById<Button>(R.id.btnSetNew)
val exitapp = findViewById<Button>(R.id.btnExit)
calen.setOnClickListener{
val intent = Intent(this, Cal::class.java)
startActivity(intent)
}
curr.setOnClickListener {
val intent = Intent( this, CurrentSch:: class.java)
startActivity(intent)
}
newpla.setOnClickListener{
val intent = Intent(this, NewPlan::class.java)
startActivity(intent)
}
exitapp.setOnClickListener {
finish()
exitProcess(0)
}
}
This is the database I have created.
class EventDatabasek(context: Context) :
SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
companion object {
private const val DATABASE_VERSION =1
private const val DATABASE_NAME ="EventDataBase"
private const val TABLE_CONTACTS = "EventTable"
private const val KEY_ID = "_id"
private const val KEY_DATE = "date"
private const val KEY_TIME = "time"
private const val KEY_REPEAT = "repeat"
private const val KEY_EMAIL = "email"
private const val KEY_NOTES = "notes"
}
override fun onCreate(db: SQLiteDatabase?){
val CREATE_CONTACTS_TABLE = (" CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + KEY_DATE + " TEXT NOT NULL ," + KEY_TIME + "TEXT NOT NULL ," + KEY_REPEAT
+ " INTEGER ," + KEY_EMAIL + " TEXT NOT NULL , " + KEY_NOTES + " TEXT NOT NULL , " +")")
db?.execSQL(CREATE_CONTACTS_TABLE)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db!!.execSQL("DROP TABLE IF EXISTS $TABLE_CONTACTS")
onCreate(db)
}
fun addEvent(evt: EventModelk): Long {
val db = this.writableDatabase
val contentValues = ContentValues()
contentValues.put(KEY_DATE, evt.dDate)
contentValues.put(KEY_TIME, evt.tTime)
contentValues.put(KEY_REPEAT, evt.repeat)
contentValues.put(KEY_EMAIL, evt.eEmail)
contentValues.put(KEY_NOTES, evt.nNotes)
val success = db.insert(TABLE_CONTACTS, null, contentValues)
db.close()
return success
}
fun veiwEvent (): ArrayList<EventModelk> {
val evtlist: ArrayList<EventModelk> = ArrayList<EventModelk>()
val selectQueue = "SELECT * FROM $TABLE_CONTACTS"
val db = this?.readableDatabase
var cursor: Cursor?
try {
cursor = db.rawQuery(selectQueue, null)
}catch (e: SQLException){
db.execSQL(selectQueue)
return ArrayList()
}
var id:Int
var date: String
var time: String
var repeat: Int
var email: String
var note: String
if (cursor.moveToFirst()){
do {
id = cursor.getInt(cursor.getColumnIndex(KEY_ID))
date = cursor.getString(cursor.getColumnIndex(KEY_DATE))
time = cursor.getString(cursor.getColumnIndex(KEY_TIME))
repeat = cursor.getInt(cursor.getColumnIndex(KEY_REPEAT))
email = cursor.getString(cursor.getColumnIndex(KEY_EMAIL))
note = cursor.getString(cursor.getColumnIndex(KEY_NOTES))
val evt = EventModelk(id = id, dDate = date, tTime = time, repeat = repeat, eEmail = email, nNotes = note)
evtlist.add(evt)
}while (cursor.moveToNext())
}
return evtlist
}
fun updateEventdata(evt: EventModelk): Int {
val db = this.writableDatabase
val contentValues = ContentValues()
contentValues.put(KEY_DATE, evt.dDate)
contentValues.put(KEY_TIME, evt.tTime)
contentValues.put(KEY_REPEAT, evt.repeat)
contentValues.put(KEY_EMAIL, evt.eEmail)
contentValues.put(KEY_NOTES, evt.nNotes)
val success = db.update(TABLE_CONTACTS, contentValues, KEY_ID + "=" + evt.id, null)
db.close()
return success
}
fun deleteEvent(evt: EventModelk): Int {
val db = this.writableDatabase
val contentValues = ContentValues()
contentValues.put(KEY_ID, evt.id)
val success = db.delete(TABLE_CONTACTS, KEY_ID + "=" +evt.id, null)
db.close()
return success
}
}
This is the error I get now
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE EventTable(_id INTEGER PRIMARY KEY AUTOINCREMENT , date TEXT NOT NULL ,timeTEXT NOT NULL ,repeat INTEGER ,email TEXT NOT NULL , notes TEXT NOT NULL , )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1805)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1733)
at com.example.scheduleit.EventDatabasek.onCreate(EventDatabasek.kt:32)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:412)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:317)
at com.example.scheduleit.MainActivity.onCreate(MainActivity.kt:28)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
The database will be created the first time that you call getReadableDatabase()
or getWritableDatabase()
in which case the onCreate()
method of your EventDatabasek
class will be invoked.
You can do this after this line:
val eventDatabasek = EventDatabasek(this)
by calling:
eventDatabasek.getReadableDatabase()