SQLite数据库从Android模拟器中消失了

著论准过秦,作赋拟子虚。这篇文章主要讲述SQLite数据库从Android模拟器中消失了相关的知识,希望能为你提供帮助。
我正在使用android Studio 3.0.1并使用Android模拟器来查看应用程序的运行方式。我的应用程序有SQLite数据库但是在更新Android Studio之后,或者它的组件模拟器启动时显示消息“执行干净启动快照不存在”。我的数据库从应用程序中消失了这是在Android模拟器上擦拭还是我在数据库编程中犯了一些错误?这种麻烦会在真实设备上发生吗?

import android.content.ContentValues import android.content.Context import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteQueryBuilder import android.widget.Toast import android.database.SQLException import kotlin.collections.ArrayListclass DbManagerVol { var dbName=Const.DB_NAME var dbTable:String? = null val dbVersion = 2 var sqlCreateTable:String? = null var sqlDB:SQLiteDatabase? = nullconstructor (context: Context, dbTable:String?, sqlCreateTable:String?) { this.sqlCreateTable = sqlCreateTable this.dbTable = dbTable val db = DataBaseHelperVol(context) sqlDB=db.writableDatabase if(sqlCreateTable!=null)sqlDB!!.execSQL(sqlCreateTable) }inner class DataBaseHelperVol:SQLiteOpenHelper { var context:Context?=null constructor(context: Context):super(context,dbName,null,dbVersion){ this.context=context } override fun onCreate(db: SQLiteDatabase?) {db!!.execSQL(sqlCreateTable) Toast.makeText(this.context, "database $dbName is created", Toast.LENGTH_LONG).show() }override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { db!!.execSQL("Drop table IF EXISTS $dbTable") }} fun dbInsert(values:ContentValues):Long{ val ID = sqlDB!!.insert(dbTable, "", values) return ID }fun dbQuerry(projection: Array< String> , selection:String, selectionArgs:Array< String> , sortOrder:String):Cursor?{ val qb = SQLiteQueryBuilder() qb.tables=dbTable var cursor:Cursor? = null try { cursor = qb.query(sqlDB, projection, selection, selectionArgs, null, null, sortOrder) }catch (e:Exception){} return cursor!!} fun dbQuerryArray(projection: Array< String> , selection:String, selectionArgs:Array< String> , sortOrder:String, spUndefined:String, spAdd:String, spDelete:String):ArrayList< String> { val listSpinner:ArrayList< String> ? = ArrayList< String> () val qb = SQLiteQueryBuilder() qb.tables=dbTabletry { val cursor:Cursor? = qb.query(sqlDB, projection, selection, selectionArgs, null, null, sortOrder) if (cursor!!.moveToFirst()) { do { val str = cursor.getString(cursor.getColumnIndex(sortOrder)) listSpinner!!.add(str)} while (cursor.moveToNext()) } listSpinner!!.add(spUndefined) listSpinner.add(spAdd) listSpinner.add(spDelete)} catch (e:SQLException) { listSpinner!!.add(spUndefined) listSpinner.add(spAdd) listSpinner.add(spDelete) }return listSpinner!! }fun dbRowDelete(colName:String, value:String):Int { sqlDB!!.execSQL(sqlCreateTable) val str2 = "$colName='$value'" return sqlDB!!.delete(dbTable, str2, null) }fun dbLastRowInt(dbTable:String, column:String):Int { val selectQuery= "SELECT * FROM $dbTable ORDER BY $column DESC LIMIT 1" val cursor = sqlDB!!.rawQuery(selectQuery, null) var a = 0 if(cursor.moveToFirst()) a=cursor.getInt( cursor.getColumnIndex(column) ) cursor.close() return a}fun dbUpdateRow (table:String, values: ContentValues, colName:String, value:Int):Int { return sqlDB!!.update(table, values, "$colName = $value", null) }fun dbDelTable () { sqlDB!!.execSQL("Drop table IF EXISTS $dbTable") }fun dbRowsDelete(colName:String, values:Array< String> ):Int { sqlDB!!.execSQL(sqlCreateTable) val placeholders = StringBuilder() for (i in 0 until values.size) { if (i != 0) placeholders.append(", ")placeholders.append("?") }val where = "$colName IN (" + placeholders.toString() + ")" return sqlDB!!.delete(dbTable, where, values) }fun countTables () :ArrayList< String> { val array = ArrayList< String> () val c = sqlDB!!.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null) if (c.moveToFirst()) { while ( !c.isAfterLast) { array.add( c.getString( c.getColumnIndex("name")) ) c.moveToNext() } } return array }}

答案你的onUpgradeDataBaseHelperVol方法只包含丢弃指令。这意味着数据库的第一个版本更新只会清理数据。可能你需要再次执行你的sqlCreateTable指令,但我不知道你想要什么。
【SQLite数据库从Android模拟器中消失了】猜猜你不懂SQLite数据库生命周期。 Try to read that。

    推荐阅读