gpt4 book ai didi

Android SQLite 泄露

转载 作者:IT王子 更新时间:2023-10-28 23:27:47 33 4
gpt4 key购买 nike

我的 sql 处理程序有问题

A SQLiteConnection object for database '/data/data/.../databases/queueManager' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

取自 Androidhive 教程并根据我的使用进行定制

table 看起来像

+ ----------------------------------------------------------- +
: DATABASE_ID : DATABASE_QID : DATABASE_QUEUE : DATABASE_DATE :
+ ----------------------------------------------------------- +

代码

   DBQueue searchDBqid(int id) {
SQLiteDatabase db = this.getReadableDatabase();

String selectQuery = "SELECT * FROM " + TABLE_QUEUE + " WHERE " + DATABASE_QID + " = " + id;

Cursor cursornum = db.rawQuery(selectQuery, null);
int dk = cursornum.getCount();
cursornum.close();

if (dk >0) {
Cursor cursor = db.query(TABLE_QUEUE, new String[] { DATABASE_ID,
DATABASE_QID, DATABASE_QUEUE, DATABASE_DATE }, DATABASE_QID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);

if (cursor != null) cursor.moveToFirst();

DBQueue dbqueue = new DBQueue(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
return dbqueue;
}

db.close();
return null;
}

DBQueue getDBQueue(int id) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_QUEUE, new String[] { DATABASE_ID,
DATABASE_QID, DATABASE_QUEUE }, DATABASE_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();

DBQueue dbqueue = new DBQueue(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
return dbqueue;
}


public String getAllqid() {
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();

String selectQuery = "SELECT * FROM " + TABLE_QUEUE + " WHERE " + DATABASE_DATE + " = '" + today.format("%d %m %Y") + "'";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

StringBuilder sb = new StringBuilder();
if (cursor.moveToFirst()) {
do {
if (sb.length() > 0) sb.append(',');
sb.append(cursor.getString(1));
} while (cursor.moveToNext());
}

String result = sb.toString();
return result;
}
public void deleteDatedDBQueue() {
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String selectQuery = "SELECT * FROM " + TABLE_QUEUE + " WHERE " + DATABASE_DATE + " != '" + today.format("%d %m %Y") + "'"; ;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
db.delete(TABLE_QUEUE, DATABASE_ID + " = ?",
new String[] { String.valueOf(Integer.parseInt(cursor.getString(0))) });
} while (cursor.moveToNext());
}
db.close();
}
public int getDBQueueCount() {
String countQuery = "SELECT * FROM " + TABLE_QUEUE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

return cursor.getCount();
}
}

谁能告诉我如何解决这个漏洞?

完整代码:http://ijailbreak.me/databasehandler.txt

最佳答案

每个 Cursor 都应该在你完成后关闭。传统的做法是:

Cursor cursor = db.query(...);
try {
// read data from the cursor in here
} finally {
cursor.close();
}

但是现在,try-with-resources ,它可以更简洁:

try (Cursor cursor = db.query(...)) {
// read data from the cursor in here
}

关于Android SQLite 泄露,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12801602/

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