我在kotlin的第一张表错误

当我尝试在Android中使用Kotlin在数据库中插入列时,我遇到了一个问题。

这是错误的:

08-04 19:45:03.781 14302-14302 / com.example.hello.note E / Zygote:v2 08-04 19:45:03.781 14302-14302 / com.example.hello.note E / Zygote:accessInfo:0 08-04 19:45:20.471 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45:20.521 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45:20.531 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45: 20.541 14302-14302 / com.example.hello.note E / MotionRecognitionManager:mSContextService = null 08-04 19:45:20.541 14302-14302 / com.example.hello.note E / MotionRecognitionManager:motionService = com.samsung.android。 motion.IMotionRecognitionService$Stub$Proxy@78b77 08-04 19:45:47.991 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45:47.991 14302- 14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45:47.991 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-0 4 19:47:40.311 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:48:14.131 14302-14302 / com.example.hello.note E / SQLiteLog:(1)没有这样的表:Notes 08-04 19:48:14.141 14302-14302 / com.example.hello.note E / SQLiteDatabase:错误插入Title = note Desc = desc android.database.sqlite.SQLiteException:no这样的表:Notes(代码1):,编译时:INSERT INTO Notes(标题,说明)VALUES(?,?)####################### ##########################################错误代码:1(SQLITE_ERROR)引起通过:SQL(查询)错误或缺少数据库。 (没有这样的表:注释(代码1):,编译时:INSERT INTO注释(标题,说明)VALUES(?,?))#################### ############################################# at android.database。在android.database.sqlite的android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:569)上的android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1004)的sqlite.SQLiteConnection.nativePrepareStatement(Native Method)。 SQLiteSession.prepare(SQLiteSession.java:588)在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:59)在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)在android.database.sqlite.SQLiteDatabase .insertWithOnConflict(SQLiteDatabase.java:1633)at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1505)at com.example.hello.note.DbManger.Insert(DbManger.kt:40)at com.example。 hello.note.addNotes.buAdd(addNotes.kt:24)在android.support.v7.app.AppCompatViewInflat的java.lang.reflect.Method.invoke(Native Method) 在android.view.View.Android.view.View.performClick(View.java:5721)上android.widget.TextView.performClick(TextView.java:10931)上的$ $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) .run(View.java:22620)在android.os.Handler.handleCallback(Handler.java:739)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper。 java:148)at android.app.ActivityThread.main(ActivityThread.java:7409)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。 java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

数据库代码:

class DbManger(context: Context) { val dbName = "MyNotes" val dbTable = "Notes" val colID = "ID" val colTitle = "Title" val colDesc = "Desc" var dbVer = 4 val sqlCreateTable = "CREATE TABLE IF NOT EXISTS " + dbTable + "(" + colID + " INTEGER PRIMARY KEY," + colTitle + " TEXT," + colDesc + " TEXT " + ");" var sqlDataBase: SQLiteDatabase? = null inner class dbHelperNotes : SQLiteOpenHelper { var context: Context? = null constructor(context: Context) : super(context, dbName, null, dbVer) { this.context = context } override fun onCreate(p0: SQLiteDatabase?) { p0!!.execSQL(sqlCreateTable) Toast.makeText(this.context, "Database is created", Toast.LENGTH_LONG).show() } override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) { p0!!.execSQL("drop table IF EXISTS " + dbTable) } } fun Insert(values: ContentValues): Long { val ID = sqlDataBase!!.insert(dbTable, "", values) return ID } init { var db = dbHelperNotes(context) sqlDataBase = db.writableDatabase } } 

@laalto是正确的,你需要重新创建你的表,当你删除它时,你需要在onUpgrade()方法中添加一个对onCreate(p0)的调用。


我在课堂上删除了一些不需要的字段,现在是这样的:

 class DbManger(context: Context) { val dbName = "MyNotes" val dbTable = "Notes" val colID = "ID" val colTitle = "Title" val colDesc = "Desc" var dbVer = 4 val sqlCreateTable = "CREATE TABLE IF NOT EXISTS $dbTable($colID INTEGER PRIMARY KEY,$colTitle TEXT,$colDesc TEXT);" var sqlDataBase: SQLiteDatabase? = null inner class dbHelperNotes(val context: Context) : SQLiteOpenHelper(context, dbName, null, dbVer) { override fun onCreate(database: SQLiteDatabase?) { database?.execSQL(sqlCreateTable) Toast.makeText(context, "Database is created", Toast.LENGTH_LONG).show() } override fun onUpgrade(database: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { database?.execSQL("drop table IF EXISTS " + dbTable) onCreate(database) } } fun insert(values: ContentValues): Long { val id = sqlDataBase!!.insert(dbTable, "", values) return id } init { val db = dbHelperNotes(context) sqlDataBase = db.writableDatabase } } 

我相信你的类名也有一个错字,应该是'DbManager'。

你的数据库版本已经是4,但你的onUpgrade只是删除表,但不创建所需的表。

如果你想要丢失数据的升级,你可以在删除旧表后在onUpgrade调用onCreate 。 或者在开发时,卸载并重新安装应用程序通常会更容易。 请参阅何时SQLiteOpenHelper onCreate()/ onUpgrade()运行? 有关SQLiteOpenHelper如何工作的更多信息。