著论准过秦,作赋拟子虚。这篇文章主要讲述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
}}
答案你的
onUpgrade
的DataBaseHelperVol
方法只包含丢弃指令。这意味着数据库的第一个版本更新只会清理数据。可能你需要再次执行你的sqlCreateTable
指令,但我不知道你想要什么。【SQLite数据库从Android模拟器中消失了】猜猜你不懂SQLite数据库生命周期。 Try to read that。
推荐阅读
- 如何在android中制作这样的动画()
- 如何让Android模拟器显示软键盘()
- Android Studio不会将x86_64显示为任何虚拟设备的选项
- 应用程序冻结在白色屏幕上,反应原生android apk
- Android模拟器不会关闭
- 获得此异常会强制Android应用程序在启动时崩溃
- create react native app expo无法连接在同一台计算机上的模拟器上运行
- (如何)Android虚拟设备可以使用本地计算机的互联网吗()
- Android Webview Back按钮适用于模拟器,但不适用于设备