gpt4 book ai didi

java - 在 android 派中重新启动应用程序时,sqlite 中输入的数据会自动删除

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:35 25 4
gpt4 key购买 nike

在 Android 派中重新启动应用程序后,SQLite 数据消失了。应用程序在其他版本上运行正常,但在 Android 派中,当您重新启动应用程序时,最后几个条目不可见。与显示其他条目一样,数据库并不完全为空。

我尝试通过禁用 WriteAheadLogging 将 WAL 模式设置为 false,这似乎在某些设备上有效,但问题并未完全解决,因为某些设备仍在报告问题,因此我尝试将 PRAGMA 日志模式设置为 TRUNCATE,但这也不起作用。

 public myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.disableWriteAheadLogging();

Cursor c1=db.rawQuery("PRAGMA journal_mode=TRUNCATE",null);
c1.close();
}


public static synchronized myDbHelper getInstance(Context context) {


if (mInstance == null) {
mInstance = new myDbHelper(context.getApplicationContext());
mInstance.setWriteAheadLoggingEnabled(false);
}
return mInstance;
}

然后在我的 fragment 中:myDbHelper dbHelper = myDbHelper.getInstance(getActivity());

我没有收到任何错误消息,只是最后几个条目没有显示,就好像它们没有保存在数据库中一样。非常感谢任何帮助。

最佳答案

我不认为该问题直接归因于 WAL 模式是否打开,而是您可能没有正确确定最后几个条目

编码mInstance.setWriteAheadLoggingEnabled(false);将在调用onConfigure本身之前直接关闭WAL(见下文)

我要做的第一件事就是改变

public myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

至:-

private myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

确保您不会无意中获得 myDBHelperrouge 实例。

作为临时措施,您还可以将以下方法添加到您的 myDBHeleper 类中:-

public void logMode(SQLiteDatabase db, String tagextra, String dbname, Context context) {
Log.d("BUILD","Build is " + Build.VERSION.SDK_INT);
File dbf = new File(context.getDatabasePath(dbname).toString());
File dbfwal = new File(dbf.getPath() + "-wal");
File dbfshm = new File(dbf.getPath() + "-shm");
if (dbfwal.exists()) {
Log.d("-WALCHK " + tagextra,"The -wal file was found an is " + String.valueOf(dbfwal.length()));
} else {
Log.d("-WALCHK " + tagextra,"The -wal file doesn't exist.");
}
if (dbfshm.exists()) {
Log.d("-SHMCHK " + tagextra,"The -shm file was found an is " + String.valueOf(dbfwal.length()));
} else {
Log.d("-SHMCHK " + tagextra,"The -shm file doesn't exist");
}
if (db == null) {
db = this.getWritableDatabase();
}
Cursor csr = db.rawQuery("PRAGMA journal_mode",null);
if (csr.moveToFirst()) {
Log.d("LOGMODE " + tagextra ,"Database mode is " + csr.getString(0));
}
}

这可以用来确定模式是否以及 -wal 和 -shm 文件是否存在并具有任何数据。

简而言之,它可以用来减轻对 WAL 模式导致的问题的担忧。

例如,使用 myDatabaseHelper 类的以下近似值。但是,除了您的代码之外:-

  • 上述 logMode 方法的调用已包含在相关点。
  • 已包含 onCreateMethod,用于创建表格并添加初始行。
  • 构造函数已设为私有(private)

所以看起来像:-

public class myDbHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "mydb";
public static final int DATABASE_VERSION = 1;

public static final String TBL_USER = "user";
public static final String COL_USER_ID = BaseColumns._ID;
public static final String COl_USER_NAME = "_name";
public static final String COL_USER_EMAIL = "_email";
public static final String COl_USER_PASSWORD = "_password";

private static myDbHelper mInstance;
private Context mContext;

private myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
logMode(this.getWritableDatabase(),"CONSTRUCT",DATABASE_NAME,context); // Note will force an open
}
@Override
public void onConfigure(SQLiteDatabase db) {
logMode(db, "ONCONFIG1", DATABASE_NAME,mContext);
super.onConfigure(db);
db.disableWriteAheadLogging();
logMode(db,"ONCONFIG2",DATABASE_NAME,mContext);
Cursor c1=db.rawQuery("PRAGMA journal_mode=TRUNCATE",null);
c1.close();
logMode(db,"ONCONFIG3",DATABASE_NAME,mContext);
}


public static synchronized myDbHelper getInstance(Context context) {

if (mInstance == null) {
mInstance = new myDbHelper(context.getApplicationContext());
mInstance.setWriteAheadLoggingEnabled(false);
}
mInstance.logMode(mInstance.getWritableDatabase(),"INSTANCE",mInstance.getDatabaseName(),context );
return mInstance;
}

@Override
public void onCreate(SQLiteDatabase db) {
logMode(db,"ONCREATE1",DATABASE_NAME,mContext);
db.execSQL("CREATE TABLE IF NOT EXISTS " + TBL_USER +
"(" +
COL_USER_ID + " INTEGER PRIMARY KEY," +
COl_USER_NAME + " TEXT UNIQUE," +
COL_USER_EMAIL + " TEXT," +
COl_USER_PASSWORD + " TEXT" +
")");
//Add a row when oncreate is called
db.execSQL("INSERT INTO " + TBL_USER + " (" +
COl_USER_NAME + "," +
COL_USER_EMAIL + "," +
COl_USER_PASSWORD +
") VALUES('testuser','testuser@mail.com','1234567890')");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public long addUser(String name, String email, String passowrd) {
ContentValues cv = new ContentValues();
cv.put(COl_USER_NAME,name);
cv.put(COL_USER_EMAIL,email);
cv.put(COl_USER_PASSWORD,passowrd);
SQLiteDatabase db = this.getWritableDatabase();
logMode(db,"ADDUSER1",DATABASE_NAME,mContext);
return db.insert(TBL_USER,null,cv);
}

public void logMode(SQLiteDatabase db, String tagextra, String dbname, Context context) {
Log.d("BUILD","Build is " + Build.VERSION.SDK_INT);
File dbf = new File(context.getDatabasePath(dbname).toString());
File dbfwal = new File(dbf.getPath() + "-wal");
File dbfshm = new File(dbf.getPath() + "-shm");
if (dbfwal.exists()) {
Log.d("-WALCHK " + tagextra,"The -wal file was found an is " + String.valueOf(dbfwal.length()));
} else {
Log.d("-WALCHK " + tagextra,"The -wal file doesn't exist.");
}
if (dbfshm.exists()) {
Log.d("-SHMCHK " + tagextra,"The -shm file was found an is " + String.valueOf(dbfwal.length()));
} else {
Log.d("-SHMCHK " + tagextra,"The -shm file doesn't exist");
}
if (db == null) {
db = this.getWritableDatabase();
}
Cursor csr = db.rawQuery("PRAGMA journal_mode",null);
if (csr.moveToFirst()) {
Log.d("LOGMODE " + tagextra ,"Database mode is " + csr.getString(0));
}
}
}

以下内容位于 fragment 的 onStart 方法中:-

@Override
public void onStart() {
super.onStart();
myDbHelper dbHelper = myDbHelper.getInstance(getActivity());
dbHelper.logMode(dbHelper.getWritableDatabase(),"FRGONSTRT1",myDbHelper.DATABASE_NAME,getActivity());
dbHelper.addUser("Fred","fred@fredmail.com","password");
Cursor csr = dbHelper.getReadableDatabase().query(myDbHelper.TBL_USER,null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();
}

就是这样:-

  1. 获取 myDBHelper 实例。
  2. 调用 logMode 方法。
  3. 添加一行。
  4. 将所有数据提取到 Cursor 中并转储 Cursor。

测试

测试 1

使用 API 28 在 Android(模拟器)上卸载应用程序并运行结果:-

2019-07-07 10:38:56.022 D/BUILD: Build is 28
2019-07-07 10:38:56.022 D/-WALCHK ONCONFIG1: The -wal file was found an is 16512
2019-07-07 10:38:56.022 D/-SHMCHK ONCONFIG1: The -shm file was found an is 16512
2019-07-07 10:38:56.023 D/LOGMODE ONCONFIG1: Database mode is wal
2019-07-07 10:38:56.038 D/BUILD: Build is 28
2019-07-07 10:38:56.038 D/-WALCHK ONCONFIG2: The -wal file doesn't exist.
2019-07-07 10:38:56.038 D/-SHMCHK ONCONFIG2: The -shm file doesn't exist
2019-07-07 10:38:56.039 D/LOGMODE ONCONFIG2: Database mode is truncate
2019-07-07 10:38:56.039 D/BUILD: Build is 28
2019-07-07 10:38:56.039 D/-WALCHK ONCONFIG3: The -wal file doesn't exist.
2019-07-07 10:38:56.039 D/-SHMCHK ONCONFIG3: The -shm file doesn't exist
2019-07-07 10:38:56.040 D/LOGMODE ONCONFIG3: Database mode is truncate
2019-07-07 10:38:56.041 D/BUILD: Build is 28
2019-07-07 10:38:56.042 D/-WALCHK ONCREATE1: The -wal file doesn't exist.
2019-07-07 10:38:56.043 D/-SHMCHK ONCREATE1: The -shm file doesn't exist
2019-07-07 10:38:56.043 D/LOGMODE ONCREATE1: Database mode is truncate
2019-07-07 10:38:56.054 D/BUILD: Build is 28
2019-07-07 10:38:56.054 D/-WALCHK CONSTRUCT: The -wal file doesn't exist.
2019-07-07 10:38:56.055 D/-SHMCHK CONSTRUCT: The -shm file doesn't exist
2019-07-07 10:38:56.055 D/LOGMODE CONSTRUCT: Database mode is truncate
2019-07-07 10:38:56.056 D/BUILD: Build is 28
2019-07-07 10:38:56.057 D/-WALCHK INSTANCE: The -wal file doesn't exist.
2019-07-07 10:38:56.057 D/-SHMCHK INSTANCE: The -shm file doesn't exist
2019-07-07 10:38:56.058 D/LOGMODE INSTANCE: Database mode is truncate
2019-07-07 10:38:56.058 D/BUILD: Build is 28
2019-07-07 10:38:56.058 D/-WALCHK FRGONSTRT1: The -wal file doesn't exist.
2019-07-07 10:38:56.058 D/-SHMCHK FRGONSTRT1: The -shm file doesn't exist
2019-07-07 10:38:56.059 D/LOGMODE FRGONSTRT1: Database mode is truncate
2019-07-07 10:38:56.059 D/BUILD: Build is 28
2019-07-07 10:38:56.059 D/-WALCHK ADDUSER1: The -wal file doesn't exist.
2019-07-07 10:38:56.059 D/-SHMCHK ADDUSER1: The -shm file doesn't exist
2019-07-07 10:38:56.060 D/LOGMODE ADDUSER1: Database mode is truncate
2019-07-07 10:38:56.069 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@4f533b6
2019-07-07 10:38:56.070 I/System.out: 0 {
2019-07-07 10:38:56.070 I/System.out: _id=1
2019-07-07 10:38:56.070 I/System.out: _name=testuser
2019-07-07 10:38:56.070 I/System.out: _email=testuser@mail.com
2019-07-07 10:38:56.070 I/System.out: _password=1234567890
2019-07-07 10:38:56.070 I/System.out: }
2019-07-07 10:38:56.070 I/System.out: 1 {
2019-07-07 10:38:56.070 I/System.out: _id=2
2019-07-07 10:38:56.070 I/System.out: _name=Fred
2019-07-07 10:38:56.070 I/System.out: _email=fred@fredmail.com
2019-07-07 10:38:56.070 I/System.out: _password=password
2019-07-07 10:38:56.070 I/System.out: }
2019-07-07 10:38:56.070 I/System.out: <<<<<

在这种情况下,由于线路

logMode(this.getWritableDatabase(),"CONSTRUCT",DATABASE_NAME,context); // Note will force an open 

之前被调用

mInstance.setWriteAheadLoggingEnabled(false);

数据库最初处于 WAL 模式,但 onConfigure 方法中的 db.disableWriteAheadLogging(); 行将模式更改为截断并提交事务(-wal 文件中存在未完成的事务,因为它的大小大于 0,(-shm 基本上是 -wal 文件的副本)),如下所示:-

2019-07-07 10:38:56.022 D/-WALCHK ONCONFIG1: The -wal file was found an is 16512

事务已提交,因为在所有后续输出中,-wal 文件已被删除。

测试 2

应用程序重新运行(即现在使用现有数据),输出为:-

2019-07-07 10:52:11.722 I/InstantRun: starting instant run server: is main process
2019-07-07 10:52:11.812 D/BUILD: Build is 28
2019-07-07 10:52:11.812 D/-WALCHK ONCONFIG1: The -wal file was found an is 0
2019-07-07 10:52:11.812 D/-SHMCHK ONCONFIG1: The -shm file was found an is 0
2019-07-07 10:52:11.813 D/LOGMODE ONCONFIG1: Database mode is wal
........... subsequent output the same as first bar times

虽然数据库最初位于 WAL 中,但没有未完成的事务(唯一的区别)。

测试 3

如果行 logMode(this.getWritableDatabase(),"CONSTRUCT",DATABASE_NAME,context);//注意将强制打开被注释掉并且应用程序被卸载并重新运行:-

2019-07-07 10:56:23.314 I/InstantRun: starting instant run server: is main process
2019-07-07 10:56:23.437 D/BUILD: Build is 28
2019-07-07 10:56:23.438 D/-WALCHK ONCONFIG1: The -wal file doesn't exist.
2019-07-07 10:56:23.438 D/-SHMCHK ONCONFIG1: The -shm file doesn't exist
2019-07-07 10:56:23.439 D/LOGMODE ONCONFIG1: Database mode is truncate
........... subsequent output the same as first bar times

然后,第一次使用(因为在db.disableWriteAheadLogging();调用之前不会强制打开)导致数据库处于TRUNCATE模式。

关于java - 在 android 派中重新启动应用程序时,sqlite 中输入的数据会自动删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56901517/

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