Android Kotlin SQLite-实时创建表

Android Kotlin SQLite-实时创建表,android,sqlite,kotlin,create-table,Android,Sqlite,Kotlin,Create Table,嗨,我有问题,当我想创建新的表,通过点击按钮像这样 buttonAddTable.setOnClickListener { db?.execSQL("CREATE TABLE ${tabela}(" + "${BaseColumns._ID} INTEGER PRIMARY KEY," + "${TableInfo.TABLE_COLUMN_

嗨,我有问题,当我想创建新的表,通过点击按钮像这样

buttonAddTable.setOnClickListener {

            db?.execSQL("CREATE TABLE ${tabela}(" +
                    "${BaseColumns._ID} INTEGER PRIMARY KEY," +
                    "${TableInfo.TABLE_COLUMN_TITLE} TEXT NOT NULL," +
                    "${TableInfo.TABLE_COLUMN_MESSAGE} TEXT NOT NULL)")
if (intent.hasExtra("tabela")) {

                var tabela = intent.getCharSequenceExtra("tabela")
                var tab = tabela.toString()
                Toast.makeText(applicationContext,"This table is: "+tabela, Toast.LENGTH_SHORT).show()
                val value = ContentValues()
                value.put(TableInfo.TABLE_COLUMN_TITLE, title)
                value.put(TableInfo.TABLE_COLUMN_MESSAGE, message)
                db.insert(tab, null, value)
if (intent.hasExtra("tabela")){

        var tabela = intent.getCharSequenceExtra("tabela")
        var tab = tabela.toString()
        val cursor = db.query(
            tab, null,
            null, null,
            null, null, null
        )
        val notes = ArrayList<Note>()
        if (cursor.count > 0) {
            cursor.moveToFirst()
            while (!cursor.isAfterLast) {
                val note = Note()
                note.id = cursor.getInt(0)
                note.title = cursor.getString(1)
                note.message = cursor.getString(2)
                notes.add(note)
                cursor.moveToNext()
            }
        }
        cursor.close()

        recyler_view.layoutManager = GridLayoutManager(applicationContext, 2)
        recyler_view.adapter = CardViewAdapter(applicationContext, db, notes)
没关系,我可以这样添加日期

buttonAddTable.setOnClickListener {

            db?.execSQL("CREATE TABLE ${tabela}(" +
                    "${BaseColumns._ID} INTEGER PRIMARY KEY," +
                    "${TableInfo.TABLE_COLUMN_TITLE} TEXT NOT NULL," +
                    "${TableInfo.TABLE_COLUMN_MESSAGE} TEXT NOT NULL)")
if (intent.hasExtra("tabela")) {

                var tabela = intent.getCharSequenceExtra("tabela")
                var tab = tabela.toString()
                Toast.makeText(applicationContext,"This table is: "+tabela, Toast.LENGTH_SHORT).show()
                val value = ContentValues()
                value.put(TableInfo.TABLE_COLUMN_TITLE, title)
                value.put(TableInfo.TABLE_COLUMN_MESSAGE, message)
                db.insert(tab, null, value)
if (intent.hasExtra("tabela")){

        var tabela = intent.getCharSequenceExtra("tabela")
        var tab = tabela.toString()
        val cursor = db.query(
            tab, null,
            null, null,
            null, null, null
        )
        val notes = ArrayList<Note>()
        if (cursor.count > 0) {
            cursor.moveToFirst()
            while (!cursor.isAfterLast) {
                val note = Note()
                note.id = cursor.getInt(0)
                note.title = cursor.getString(1)
                note.message = cursor.getString(2)
                notes.add(note)
                cursor.moveToNext()
            }
        }
        cursor.close()

        recyler_view.layoutManager = GridLayoutManager(applicationContext, 2)
        recyler_view.adapter = CardViewAdapter(applicationContext, db, notes)
很好,但当我想这样展示这张桌子的时候

buttonAddTable.setOnClickListener {

            db?.execSQL("CREATE TABLE ${tabela}(" +
                    "${BaseColumns._ID} INTEGER PRIMARY KEY," +
                    "${TableInfo.TABLE_COLUMN_TITLE} TEXT NOT NULL," +
                    "${TableInfo.TABLE_COLUMN_MESSAGE} TEXT NOT NULL)")
if (intent.hasExtra("tabela")) {

                var tabela = intent.getCharSequenceExtra("tabela")
                var tab = tabela.toString()
                Toast.makeText(applicationContext,"This table is: "+tabela, Toast.LENGTH_SHORT).show()
                val value = ContentValues()
                value.put(TableInfo.TABLE_COLUMN_TITLE, title)
                value.put(TableInfo.TABLE_COLUMN_MESSAGE, message)
                db.insert(tab, null, value)
if (intent.hasExtra("tabela")){

        var tabela = intent.getCharSequenceExtra("tabela")
        var tab = tabela.toString()
        val cursor = db.query(
            tab, null,
            null, null,
            null, null, null
        )
        val notes = ArrayList<Note>()
        if (cursor.count > 0) {
            cursor.moveToFirst()
            while (!cursor.isAfterLast) {
                val note = Note()
                note.id = cursor.getInt(0)
                note.title = cursor.getString(1)
                note.message = cursor.getString(2)
                notes.add(note)
                cursor.moveToNext()
            }
        }
        cursor.close()

        recyler_view.layoutManager = GridLayoutManager(applicationContext, 2)
        recyler_view.adapter = CardViewAdapter(applicationContext, db, notes)
显示行

val dbHelper = DataBaseHelper(applicationContext)
    val db = dbHelper.writableDatabase
    if (intent.hasExtra("tabela")){

        var tabela = intent.getCharSequenceExtra("tabela")
        var tab = tabela.toString()
        val cursor = db.query(
            tab, null,
            null, null,
            null, null, null
        )
        val notes = ArrayList<Note>()
        if (cursor.count > 0) {
            cursor.moveToFirst()
            while (!cursor.isAfterLast) {
                val note = Note()
                note.id = cursor.getInt(0)
                note.title = cursor.getString(1)
                note.message = cursor.getString(2)
                notes.add(note)
                cursor.moveToNext()
            }
        }
        cursor.close()

        recyler_view.layoutManager = GridLayoutManager(applicationContext, 2)
        recyler_view.adapter = CardViewAdapter(applicationContext, db, notes)

    }else {

        val cursor = db.query(
            TableInfo.TABLE_NAME, null,
            null, null,
            null, null, null
        )
        val notes = ArrayList<Note>()


        if (cursor.count > 0) {
            cursor.moveToFirst()
            while (!cursor.isAfterLast) {
                val note = Note()
                note.id = cursor.getInt(0)
                note.title = cursor.getString(1)
                note.message = cursor.getString(2)
                notes.add(note)
                cursor.moveToNext()
            }
        }
        cursor.close()

        recyler_view.layoutManager = GridLayoutManager(applicationContext, 2)
        recyler_view.adapter = CardViewAdapter(applicationContext, db, notes)

    }

}

您好,您可以通过cursor.getColumnNames迭代cursor的列并向我们显示日志吗?您确定可以在表中插入新行吗?您是否检查了
db.insert(tab,null,value)
的结果值?我有一个表,它是在第一次启动应用程序时创建的。现在,当我创建新表和新行时,我可以显示这个新表,但当我将行添加到表“Notes”时,我仍然可以看到这个错误java.lang.IndexOutOfBoundsException:Index:3,Size:3。我现在编辑帖子,你几乎可以看到我发现的所有错误。In override fun getItemCount():我仍然使用存储在变量中的相同表名。搜索3天时出现轻微错误:/