gpt4 book ai didi

java - Android sqlite 日志报告和 db.close()

转载 作者:太空狗 更新时间:2023-10-29 15:49:22 25 4
gpt4 key购买 nike

日志报告数据库或游标未关闭。我基本上有一个带有自定义表面 View 的 Activity ,并使用处理程序向该 Activity 报告。当我收到消息时,我会显示一个警报对话框并更新数据库。

private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case 1:
dh.open();
dh.updateEvent("id", "name", "someone");
dh.close();
successAlert.show();
break;
case 2:
failAlert.show();
break;
}
}
};

以前我没有“dh.close()”,那是当日志报告数据库/游标未关闭时。但是自从我把它加进去之后,真的需要很长时间才能完成。收到消息后,系统似乎挂起。我做错了什么还是通常需要这么长时间。我还尝试使用带有 finally 的 try block 来关闭数据库。

编辑:

public class DatabaseHelper {

private Database dbHelper;
private SQLiteDatabase db;
private Context context;

public DatabaseHelper(Context context) {
this.context = context;
//database = new Database(context);
}

public void open() {
dbHelper = new Database(context);
db = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public void updateEvent(int id, String name, int other) {
ContentValues cv = new ContentValues();
cv.put("id", id);
cv.put("name", name);
cv.put("other", other);
db.update("stateTable", cv, "id=" + id, null);
}

public boolean checkState(int id) {
db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("stateTable", null, null, null, null, null, null);
cursor.moveToPosition(id - 1);
int i = cursor.getInt(2);
android.util.Log.d("semajhan", ": " + i);
if (i == 1) {
return true;
} else {
return false;
}
}

扩展的 SQLiteOpenHelper:

public class Database extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "stateTable";
private static final String ID = "id";
private static final String NAME = "name";
private static final String OTHER = "other";
private static final String DATABASE_CREATE = "CREATE TABLE stateTable (id INT, name TEXT, other INT)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS table";

public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_UPGRADE);
onCreate(db);
}

01-11 13:57:41.239: ERROR/ActivityManager(61): ANR in com.semajhan.soodles  (com.semajhan.soodles/.Level1)
01-11 13:57:41.239: ERROR/ActivityManager(61): Reason: keyDispatchingTimedOut
01-11 13:57:41.239: ERROR/ActivityManager(61): Load: 1.64 / 0.56 / 0.26
01-11 13:57:41.239: ERROR/ActivityManager(61): CPU usage from 35716ms to -1ms ago:
01-11 13:57:41.239: ERROR/ActivityManager(61): 44% 862/com.semajhan.soodles: 37% user + 7.2% kernel / faults: 853 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 29% 61/system_server: 27% user + 1.9% kernel / faults: 142 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.2% 731/com.android.quicksearchbox: 0% user + 0.2% kernel / faults: 30 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.2% 707/com.android.launcher: 0.2% user + 0% kernel / faults: 30 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.2% 801/com.svox.pico: 0.1% user + 0.1% kernel / faults: 363 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 0% 117/com.android.systemui: 0% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 0% 41/adbd: 0% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 99% TOTAL: 86% user + 13% kernel + 0% irq
01-11 13:57:41.239: ERROR/ActivityManager(61): CPU usage from 1969ms to 2620ms later:
01-11 13:57:41.239: ERROR/ActivityManager(61): 54% 61/system_server: 48% user + 6% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 40% 69/SurfaceFlinger: 40% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 10% 92/InputDispatcher: 7.5% user + 3% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 1.5% 62/HeapWorker: 1.5% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 44% 862/com.semajhan.soodles: 32% user + 12% kernel / faults: 2 minor
01-11 13:57:41.239: ERROR/ActivityManager(61): 24% 874/Thread-13: 24% user + 0% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 23% 862/studios.soodles: 4.6% user + 18% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 1.5% 867/Compiler: 0% user + 1.5% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.8% 731/com.android.quicksearchbox: 0% user + 0.8% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 0.8% 732/HeapWorker: 0% user + 0.8% kernel
01-11 13:57:41.239: ERROR/ActivityManager(61): 100% TOTAL: 76% user + 23% kernel

最佳答案

第一次调用 SQLiteOpenHelper 实例的 getReadableDatabasegetWritableDatabase 需要很长时间才能完成。您不应该在每次需要查询数据库时都创建一个新的 Database 对象(您的 SQLiteOpenHelper 实例)。尝试在 DatabaseHelper 中使用相同的 Database 实例。

当使用 SQLiteOpenHelper 时,您不想关闭 SQLiteOpenHelperSQLiteDatabase 对象,因为它是共享的;即 getWritableDatabase 总是返回相同的 SQLiteDatabase 对象。

请注意您的 checkState 方法泄漏了一个游标。为了帮助防止游标泄漏,我总是在获取游标后使用 try-finally。例如:

    db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("stateTable", null, null, null, null, null, null);
try {
cursor.moveToPosition(id - 1);
int i = cursor.getInt(2);
android.util.Log.d("semajhan", ": " + i);
if (i == 1) {
return true;
} else {
return false;
}
} finally {
cursor.close();
}

关于java - Android sqlite 日志报告和 db.close(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660363/

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