英文:
Values are getting stored in .db-shm and .db-wal instead of actual SQLite file while INSERT in Android Studio
问题
我正在向存储在data>data>package_name>database
文件中的现有数据库插入值。但是在插入值时,与其存储在data.db
文件中,这些值会自动创建data.db-shm
和data.db-wal
文件,并保存在其中。我无法访问.db-shm
和.db-wal
文件,但我可以从修改时间看出,每当我插入值时,这两个文件都会被修改,而不是实际的数据库文件。
以下是我的代码 -
MainActivity.java
package com.data.wifi;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;
import java.util.ArrayList;
public class MainActivity extends BridgeActivity {
DBHelper dbHelper = new DBHelper(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL("INSERT INTO applogs VALUES ('7760','Tue Oct 27 2020','Values inserted Successfully' )");
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
}});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.clear();
}
}
DBHelper.java
package com.data.wifi;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "data.db";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
每当我尝试运行这段代码时,会创建两个具有相同数据库名称但不同扩展名(.db-shm和.db-shm)的文件,并且从修改时间来看,似乎一直在修改这两个文件。在此图片中,wifi.db是实际的数据库文件。但是会自动创建两个其他文件,看起来只有这两个文件会被修改
附注:在模拟器中,这段代码运行得非常完美,只有在我尝试在实际移动设备上运行时才会出现此问题。
编辑 - 当我在模拟器中运行代码时,值会被正确地插入到原始的数据库文件中,不会创建此类shm和wal文件。我认为这是一个问题,因为当我尝试提取我的数据库文件时,我在其中看不到我插入的值,数据库文件也没有被修改过。
英文:
I am inserting values to an Existing database stored in data>data>package_name>database
file. but while Inserting the values , Instead of getting stored in data.db
file, the values are auto-creating data.db-shm
and data.db-wal
files , and getting saved in it. I cant access the .db-shm and .db-wal file , but I can see from the modification time , that whenever I insert values these two files are modified not the actual one.
Below are my code -
MainActivity.java
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
package com.data.wifi;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;
import java.util.ArrayList;
public class MainActivity extends BridgeActivity {
DBHelper dbHelper = new DBHelper(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL("INSERT INTO applogs VALUES ('7760','Tue Oct 27 2020','Values inserted Successfully' )");
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
}});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.clear();
}
}
<!-- end snippet -->
DBHelper.java
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
package com.data.wifi;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "data.db";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
<!-- end snippet -->
Whenever I am trying to run this code, two files are being created with same db name with different extensions (.db-shm and .db-shm) and it from the modification time , It looks like these two file are the ones getting modified all the time.In this pic wifi.db is the actual database file . but two other files are created automatically and looks like it is modifying only
PS- This code is working absolutely perfect in emulator, only when I try to run it on actual mobile device I am facing this issue.
Edit- When I run the code in an emulator , the values are inserted in the original dbfile properly, and no such shm and wal files are created there .
I am seeing this is an issue , because when I try to extract my db file , i dont see my inserted values there. neithr the db file looks modified
答案1
得分: 2
这种行为是正常的。这是预写日志工作的方式。如果您想要备份或将数据库传输到其他地方,
您可以执行
pragma wal_checkpoint
以确保所有插入的值都存储在主要的(.db3)文件中。
或者您可以使用
pragma journal_mode=DELETE
参见这个问题:
https://stackoverflow.com/questions/51535178/how-to-manually-perform-checkpoint-in-sqlite-android
以及这个:
https://www.sqlite.org/pragma.html#pragma_journal_mode
英文:
This behavior is normal. This is the way write-ahead logging works. If you want to backup or transfer the database to some other place,
you can execute
pragma wal_checkpoint
to ensure that all your inserted values are stored in the main (.db3) file.
Or you can use
pragma journal_mode=DELETE
See this question:
https://stackoverflow.com/questions/51535178/how-to-manually-perform-checkpoint-in-sqlite-android
and this:
https://www.sqlite.org/pragma.html#pragma_journal_mode
答案2
得分: 1
以下是翻译好的部分:
从 .db-wal
文件移动数据到 .db
文件,请使用 PRAGMA wal_checkpoint 命令,以下代码对我有效:
SQLiteDatabase db = this.getWritableDatabase();
String query = "PRAGMA wal_checkpoint(full)";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
int a = cursor.getInt(0);
int b = cursor.getInt(1);
int c = cursor.getInt(2);
}
if (cursor != null) {
cursor.close();
}
英文:
To move your data from .db-wal to .db use PRAGMA wal_checkpoint, following code worked for me -:
SQLiteDatabase db = this.getWritableDatabase();
String query = "PRAGMA wal_checkpoint(full)";
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
int a = cursor.getInt(0);
int b = cursor.getInt(1);
int c = cursor.getInt(2);
}
if (cursor != null) {
cursor.close();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论