我制作了一款包含3项活动的应用。 在fisrt活动(导入)中,我只是将一些值导入sqlite数据库。
这是我的DatabaseHelper类:
public class DatabaseHelper_bp extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "bpDB";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table bp_import ( _id integer primary key, datetime text not null, systolic text not null, diastolic text not null, pulses text not null, notes text not null);";
public DatabaseHelper_bp(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
// Method is called during an upgrade of the database,
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.w(DatabaseHelper_bp.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS bp_import");
onCreate(database);
}
}
我的DAO课程用于我的度量/值:
public class BpDAO {
private DatabaseHelper_bp dbHelper;
private SQLiteDatabase database;
/**
* bp table related constants.
*/
public final static String bp_TABLE = "bp_import";
public final static String bp_ID = "_id";
public final static String bp_DT = "datetime";
public final static String bp_SYS = "systolic";
public final static String bp_DIA = "diastolic";
public final static String bp_PUL = "pulses";
public final static String bp_NOT = "notes";
/**
*
* @param context
*/
public BpDAO(Context context) {
dbHelper = new DatabaseHelper_bp(context);
database = dbHelper.getWritableDatabase();
}
/**
* \ Creates a new blood pressure measure
*
* @param datetime
* @param systolic
* @param diastolic
* @param pulses
* @param notes
* @return
*/
public long importBP(String datetime, String systolic, String diastolic,
String pulses, String notes) {
ContentValues values = new ContentValues();
values.put(bp_DT, datetime);
values.put(bp_SYS, systolic);
values.put(bp_DIA, diastolic);
values.put(bp_PUL, pulses);
values.put(bp_NOT, notes);
return database.insert(bp_TABLE, null, values);
}
public void close() {
database.close();
}
/**
* Fetch all bp
*
* @return
*/
public Cursor fetchAll_bp() {
Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
bp_DIA, bp_DT, bp_ID }, null, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
在第二个活动(历史记录)中,我有一个由数据库填充的List,所有ok
以下是2活动(历史)的代码:
public class HistoryActivity extends ListActivity {
private BpDAO dao;
private SimpleCursorAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dao = new BpDAO(this);
Cursor bpList = dao.fetchAll_bp();
String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
R.id.bpDtHolder };
dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
from, target);
setListAdapter(dbAdapter);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, view, position, id);
Log.d("BPT", "Selected bp id =" + id);
// log says that i have selected an item with id : 11
Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
String bp_DT = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_DT));
String bp_SYS = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_SYS));
String bp_DIA = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_DIA));
String bp_PUL = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_PUL));
String bp_NOT = selectedBpDetails.getString(selectedBpDetails
.getColumnIndex(BpDAO.bp_NOT));
Log.d("BPT", "Selected bp details = { date=" + bp_DT + ", systolic="
+ bp_SYS + ", diastolic=" + bp_DIA + ", pulses=" + bp_PUL
+ ", notes=" + bp_NOT + " }");
Intent intent = new Intent(HistoryActivity.this, FromHistory.class);
intent.putExtra("bp_SYS", bp_SYS);
intent.putExtra("bp_DIA", bp_DIA);
intent.putExtra("bp_DT", bp_DT);
intent.putExtra("bp_PUL", bp_PUL);
intent.putExtra("bp_NOT", bp_NOT);
startActivity(intent);
}
}
当我点击列表项时,我开始一个新的活动,我从数据库中显示该度量的所有信息。
但我有logcat:
1.close() was never explicitly called on database
2.android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
3.E/System(561): java.lang.IllegalStateException: Don't have database lock!
我已经看到了其他一些问题,但未能找到如何根据我的情况关闭它。
答案 0 :(得分:1)
完成光标后,您需要close()
示例:
selectedBpDetails.close();
答案 1 :(得分:1)
您可以通过调用数据close()
来直接关闭数据,也可以覆盖活动的onDestroy
方法,然后在执行此方法时关闭你打开过的光标。
public class HistoryActivity extends ListActivity {
// ...
Cursor bpList;
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
bpList = dao.fetchAll_bp();
// ...
}
@Override
protected void onDestroy() {
super.onDestroy();
bpList.close();
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
// ...
Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
// ...
selectedBpDetails.close();
}
// ...
}
答案 2 :(得分:1)
(当你完成游标时,你应该关闭游标,正如其他人指出的那样,但是......)
该错误告诉您需要关闭SQLiteDatabase。将此方法添加到BpDAO
类:
public void close() {
database.close();
}
每当您在任何活动中创建新的BpDAO
对象时,您需要致电close()
,您可以在完成后或onDestroy()
内完成此操作:
@Override
protected void onDestroy() {
super.onDestroy();
dao.close();
}