【问题标题】:In my SQliteDatabase date is not createing (Kotlin)在我的 SQlite 数据库中,日期未创建(Kotlin)
【发布时间】:2021-10-15 04:28:18
【问题描述】:

我在 android studio 中有一个 SqlitieDatebase,由于某种原因,数据库没有创建。如果有人可以帮助我解决这个问题,我真的很喜欢。

我没有得到数据库的任何错误。

感谢您的帮助

这是我刚刚启动数据库的代码

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)

【问题讨论】:

  • 这是用于修复它的代码:
  • val CREATE_CONTACTS_TABLE = (" CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY ," + 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)

标签: android sqlite kotlin android-sqlite sqliteopenhelper


【解决方案1】:

数据库将在您第一次调用getReadableDatabase()getWritableDatabase() 时创建,在这种情况下,您的EventDatabasek 类的onCreate() 方法将被调用。

您可以在此行之后执行此操作:

val eventDatabasek = EventDatabasek(this)

通过调用:

eventDatabasek.getReadableDatabase()

【讨论】:

  • 我更新了错误和我现在得到的新错误我已经做出了改变
  • @WilliamB 删除 CREATE TABLE 语句的最后一个 ,
  • @WilliamB 也在TEXT 之前添加一个空格:KEY_TIME + "TEXT NOT NULL,所以它变成KEY_TIME + " TEXT NOT NULL。从设备上卸载应用程序并重新运行。
猜你喜欢
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
相关资源
最近更新 更多