1
我正在处理我的第一个Android应用程序,并且遇到了一个问题,我正在重复使用很多代码片段来执行数据库连接。我看到自己一遍又一遍地重写相同的try/catch语句,但是我遇到了处理它的好方法。请看看这段代码,你是否看到我缺少如何重构这个的明显方式?是否有重构此SQLite数据库处理程序的有效方法?
正如你所看到的那样,这些方法通常只有1或2条线差异。下面是一个示例(所有其他try/catch代码都是类似的):
游标cursor = dbAdapter.fetchReceipts(timeFrom,timeTo);
Communicator.java
public ArrayList<Receipt> getReceipts(int limit)
{
ArrayList<Receipt> receiptList = null;
DbAdapter dbAdapter = new DbAdapter(context);
try
{
dbAdapter.open();
Cursor cursor = dbAdapter.fetchReceipts(limit);
if (cursor != null)
{
receiptList = buildReceiptList(cursor);
}
dbAdapter.close();
}
catch (SQLException e)
{
Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
showToast(MESSAGE_COULD_NOT_OPEN);
}
return receiptList;
}
public ArrayList<Receipt> getReceipts(long timeFrom, long timeTo)
{
ArrayList<Receipt> receiptList = null;
DbAdapter dbAdapter = new DbAdapter(context);
try
{
dbAdapter.open();
Cursor cursor = dbAdapter.fetchReceipts(timeFrom, timeTo);
if (cursor != null)
{
receiptList = buildReceiptList(cursor);
}
dbAdapter.close();
}
catch (SQLException e)
{
Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
showToast(MESSAGE_COULD_NOT_OPEN);
}
return receiptList;
}
public Receipt getLatestReceipt()
{
Receipt receipt = null;
Cursor cursor = null;
DbAdapter dbAdapter = new DbAdapter(context);
try
{
dbAdapter.open();
cursor = dbAdapter.fetchLastReceipt();
if (cursor.getCount() > 0)
{
receipt = buildReceipt(cursor);
}
dbAdapter.close();
}
catch (SQLException e)
{
Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
showToast(MESSAGE_COULD_NOT_OPEN);
}
return receipt;
}
public ArrayList<Receipt> searchReceipts(String query)
{
ArrayList<Receipt> receiptList = null;
DbAdapter dbAdapter = new DbAdapter(context);
try
{
dbAdapter.open();
Cursor cursor = dbAdapter.searchReceiptName(query);
if (cursor != null)
{
receiptList = buildReceiptList(cursor);
}
dbAdapter.close();
}
catch (SQLException e)
{
Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
showToast(MESSAGE_COULD_NOT_OPEN);
}
return receiptList;
}
public boolean updateReceipt(Receipt receipt)
{
DbAdapter dbAdapter = new DbAdapter(context);
boolean result = false;
try
{
dbAdapter.open();
result = dbAdapter.updateReceipt(receipt.getId(), receipt.getName(), receipt.getPhoto(), receipt.getTimestamp(),
receipt.getLocationLat(), receipt.getLocationLong(), receipt.getSum(), receipt.getTax(), receipt.getComment());
showResult(result);
dbAdapter.close();
}
catch (SQLException e)
{
Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
showToast(MESSAGE_COULD_NOT_OPEN);
}
return result;
}
private boolean insertReceipt(Receipt receipt)
{
boolean result = false;
DbAdapter dbAdapter = new DbAdapter(context);
try
{
dbAdapter.open();
result = dbAdapter.createReceipt(receipt.getName(), receipt.getPhoto(), receipt.getTimestamp(), receipt.getLocationLat(),
receipt.getLocationLong(), receipt.getSum(), receipt.getTax(), receipt.getComment());
showResult(result);
dbAdapter.close();
}
catch (SQLException e)
{
Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
showToast(MESSAGE_COULD_NOT_OPEN);
}
return result;
}
DBAdapter.java
public Cursor fetchReceipt(long rowId) throws SQLException
{
Cursor cursor = db
.query(true, DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP, KEY_LOCATION_LAT,
KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
public Cursor fetchReceipts(long timeFrom, long timeTo)
{
Cursor cursor = db.query(true, DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP,
KEY_LOCATION_LAT, KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, KEY_TIMESTAMP + ">" + timeFrom + " AND "
+ KEY_TIMESTAMP + "<" + timeTo, null, null, null, KEY_TIMESTAMP + " DESC", null);
if (cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
public Cursor fetchLastReceipt()
{
Cursor cursor = db.query(true, DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP,
KEY_LOCATION_LAT, KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, null, null, null, null, KEY_ROWID + " DESC", "1");
if (cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
public Cursor searchReceiptName(String query)
{
Cursor cursor = db.query(DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP, KEY_LOCATION_LAT,
KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, KEY_NAME + " LIKE ?", new String[] { "%" + query + "%" }, null, null,
null);
if (cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
感谢您的回复,但我不确定这确实会带来很大的不同。我真正想要的是重用try/catch部分,因为这是大部分重复的地方。将光标移动到参数中仍然意味着很多重复:/ – span 2012-08-13 15:27:56
好吧,看起来相同的所有代码现在只是写入了。每次通话都会改变你的光标。你在这里提到什么重复?你有什么建议? 我看了一下你的SQLite查询来找到一些优化的机会,但它们看起来足够大,可以分开。当我与数据库进行通信时,通常会做什么我使用一种获取方法并使用另一种方法来编写参数化干草SQLiteDatabase.query。这对你来说可能也是一个研究点。 – 2012-08-13 19:37:40
谢谢Jose,下次我允许自己进行一些重构时,我会更深入地研究这一点。会让你知道它是如何发生的:) – span 2012-08-15 07:30:14