- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 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);
}
确保您不会无意中获得 myDBHelper 的 rouge 实例。
作为临时措施,您还可以将以下方法添加到您的 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 类的以下近似值。但是,除了您的代码之外:-
所以看起来像:-
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();
}
就是这样:-
使用 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 文件已被删除。
应用程序重新运行(即现在使用现有数据),输出为:-
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 中,但没有未完成的事务(唯一的区别)。
如果行 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/
(“ donut 派”)是否有最大切片数?我使用了 highchart(“ donut 派”)示例,但无法超过 10 个切片。请注意,内部切片“F”丢失了。下面是示例代码。 http://jsfidd
有谁知道如何找到 Samsung Galaxy 手机等同于 Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS 的常量?我有一个应用程序在三星用户单击应该将他们带到“
所以我很难弄清楚如何设置饼图/图例位置的布局。我希望图例左对齐,饼图右对齐。 40/60 百分比 -ish。 像这样: chart = new Highcharts.Chart({ c
我试图创建一种带有 4 个切片的圆形旋转木马,通过单击一个切片,它会扩展到约 2/3 的饼图,显示它的内容(另一个切片同样会收缩)基本上我是从 raphael “growing pie” 演示开始的
我尝试了所有可能的方式来提供 PIE.js 的路径,我使用的是 asp.net,这与在 java+tomcat 中的工作方式相同。我试过这样给 1)文件:css/home.css,css/pie/PI
cmds = ['time'] while True: inp = input('::> ') sinp = inp.split() if str(sinp[0]) in cm
我已经在 Raphael Google Groups 上看到过这个问题,但经过数小时的搜索后,在这里以及 Google 上,我似乎找不到解决方案。 我只是希望能够使用 jQuery 定位我的饼图(sv
这个问题在这里已经有了答案: Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE (5
我是一名优秀的程序员,十分优秀!