我在android工作室有一个sqlitedatebase,由于某种原因,数据库没有创建。如果有人能帮我解决这个问题,我真的很高兴。
我没有得到一个错误的所有数据库。
谢谢你的帮助
这是我用来启动数据库的代码
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)
}
}
这是我创建的数据库。
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
}
}
这是我现在得到的错误
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)
数据库将在您第一次调用getReadableDatabase()
或getWritableDatabase()
时创建,在这种情况下,您的EventDatabasek
类的onCreate()
方法将被调用。
你可以在这行之后这样做:
val eventDatabasek = EventDatabasek(this)
通过调用:
eventDatabasek.getReadableDatabase()