2012-08-12 98 views
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; 
    } 

回答

1

首先,打开数据库的时候,我会更好地使用这样的:

if (db == null || !db.isOpen()) { 
db = getWritableDatabase(); //getReadableDatabase(); 
} 

,它会照顾你创建或打开数据库,以防尚未完成。

对于方法的第一块我想清楚地去somethink像:

public ArrayList<Receipt> searchReceipts(String query, DbAdapter _db, Cursor cursor) 
{ 
    ArrayList<Receipt> receiptList = null; 

    try 
    { 
     _db.open(); 

     if (cursor != null) 
     { 
      receiptList = buildReceiptList(cursor); 
     } 

     _db.close(); 
    } 
    catch (SQLException e) 
    { 
     Log.d(context.getString(R.string.tag_receipttracker), e.getMessage()); 
     showToast(MESSAGE_COULD_NOT_OPEN); 
    } 

    return receiptList; 
} 

这样你只需要在之前创建适配器和光标在main方法传递下去他们,将执行所有呼叫的其余逻辑。 我注意到有一种方法只是返回一个对象而不是数组。在这种情况下,您可以传递一个内部唯一对象的数组。

希望它有帮助。

让我知道你的印象/更新。

+0

感谢您的回复,但我不确定这确实会带来很大的不同。我真正想要的是重用try/catch部分,因为这是大部分重复的地方。将光标移动到参数中仍然意味着很多重复:/ – span 2012-08-13 15:27:56

+1

好吧,看起来相同的所有代码现在只是写入了。每次通话都会改变你的光标。你在这里提到什么重复?你有什么建议? 我看了一下你的SQLite查询来找到一些优化的机会,但它们看起来足够大,可以分开。当我与数据库进行通信时,通常会做什么我使用一种获取方法并使用另一种方法来编写参数化干草SQLiteDatabase.query。这对你来说可能也是一个研究点。 – 2012-08-13 19:37:40

+0

谢谢Jose,下次我允许自己进行一些重构时,我会更深入地研究这一点。会让你知道它是如何发生的:) – span 2012-08-15 07:30:14

相关问题