gpt4 book ai didi

java - 每次应用程序重新启动时都会重新创建 SQLite 数据库表

转载 作者:行者123 更新时间:2023-12-01 05:14:59 24 4
gpt4 key购买 nike

我是 Android 开发新手。我的应用程序有问题。我的 SQLite 数据库表每次系统重新启动时都会重新创建,并且所有保存的内容都会从表中清除。谁能帮我解决这个问题吗?

这是我的数据库类......

package Nsh.android.sms;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.SyncStateContract.Helpers;

public class Database {

public static final String MYDATABASENAME="Mylanguagedbnew.db";
public static final String MYDATABASETABLE="Mylanguagedb_table1";
public static final int MYDATABASEVERSION=1;
public static final String KEY_id="_id";
public static final String KEY_language="languages";
public static final String KEY_description="languagedescription";
private static final String SCRIPT_CREATE_DATABASE="create table if not exists
"+MYDATABASETABLE+" ("+KEY_id+" integer primary key autoincrement, "+KEY_language+"
text not null, "+KEY_description+ " text not null);";
private datahelper sqliteopenhelper;
public static String userselectedlang;
private SQLiteDatabase sqlitedatabase;
private Context context;
public Database(Context c){
System.out.println("DBcontext!!!1111");
context=c;
}
public Database openToread()throws android.database.SQLException{
System.out.println("DBINSERTTTTTTTTT!!!OPENREAD22222");
sqliteopenhelper=new datahelper(context,MYDATABASENAME,null,MYDATABASEVERSION);
System.out.println("DBINSERTTTTTTTTT!!!OPENREAD22222");
sqlitedatabase=sqliteopenhelper.getReadableDatabase();
System.out.println("DBINSERTTTTTTTTT!!!OPENREAD22222");
return this;
}
public Database openTowrite()throws android.database.SQLException{
System.out.println("DBINSERTTTTTTTTT!!!OPENWRITE22222");
sqliteopenhelper=new datahelper(context,MYDATABASENAME,null,MYDATABASEVERSION);
sqlitedatabase=sqliteopenhelper.getWritableDatabase();
return this;
}
public void close(){
sqliteopenhelper.close();
}
public Cursor retriveall(){
System.out.println("RETRIEVE ALL OK!!!!!!!!!");
String[] columns={KEY_id,KEY_language,KEY_description};
Cursor cursor=sqlitedatabase.query(MYDATABASETABLE, columns,null,null,null,null,null);
return cursor;
}
public Cursor retrivelanguages(){
String[] columns={KEY_language};
Cursor c=sqlitedatabase.query(MYDATABASETABLE, columns,null,null,null,null,null);
return c;
}
public Cursor retrieveselectedlanguagedecription(){
userselectedlang=listlang.selectedlanguage;
String query="select languagedescription from Mylanguagedb_table1 where languages
='"+userselectedlang+"';";
Cursor c1=sqlitedatabase.rawQuery(query,null);
return c1;
}
public long insert(String language,String languagedescription){
System.out.println("DBINSERTTTTTTTTT!!!INSERT !!!!!!!!!1111");
ContentValues contentvalues=new ContentValues();
contentvalues.put(KEY_language,language);
contentvalues.put(KEY_description,languagedescription);
return sqlitedatabase.insert(MYDATABASETABLE, null,contentvalues);
}
public int deleteall(){

return sqlitedatabase.delete(MYDATABASETABLE,null,null);
}
/*public int deleterow(){
String selectedrow=listlang.selectedlanguage;

return sqlitedatabase.delete(MYDATABASETABLE,selectedrow, null);
}*/
private static class datahelper extends SQLiteOpenHelper{

public datahelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
//db.update(table, values, whereClause, whereArgs)
}

}
}

最佳答案

当应用程序启动时,它将创建新的数据库和表(如果不存在),否则它将继续退出数据库。

将此代码放入应用的启动 Activity 的 onCreate() 方法中,以检查数据库是否已存在。

//this is  the name of my database
public static final String DATABASE_NAME = "mydatabase.db";

//this is the complete path of my database stored. you can check in DDMS
public static final String DATABASE_PATH = "/data/data/com.rdc.mydatabase/databases/";

Context context = getBaseContext();

boolean databaseStatus = checkDataBase( DATABASE_PATH,DATABASE_NAME);
//this will check when app start
if (databaseStatus == false) {

DBAdapter database = new DBAdapter(context);
//here you can call your create table method
//database.createLoginTable();
Log.v("Debug", "Login table created.");
}
else{
Log.v("Debug", "Database already exist");
}

//method to check whether db is already exist or not and returns boolean
private boolean checkDataBase(String databasePath, String databaseName) {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(databasePath + databaseName,
null, SQLiteDatabase.OPEN_READONLY);
checkDB.close();
Log.v("Debug", "Database exist");
return true;
} catch (SQLiteException e) {
Log.v("Debug", "Database not exist");

}
return false;
}

希望对你有帮助!!

关于java - 每次应用程序重新启动时都会重新创建 SQLite 数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com