0
我正在尝试做出非常简单的东西,但是我得出的结论是我的代码看起来非常难看,而且我确信有一些方法可以改善代码的性能和清晰度。尽可能减少和改善SQL语句的代码
我该如何使用Android SQLITE中的SQL语句来执行此操作?
我有两个表:A和B. 在一个表列d,p,u。并在表B列d,p,u。这些列具有相同的名称。
我需要找到每个表中列p是某个值的最后一个添加行。和更新列d和u从表B中的列d的值和u从表A
final String selection = A.p + SQL_LIKE;
final String[] selectionArgs = new String[]{phone};
final String sortOrder = A.d + " DESC LIMIT 1";
final Cursor cursorA = getContentResolver().query(URI_A,
null, selection, selectionArgs, sortOrder);
if (cursorA != null && cursorA.moveToFirst()) {
final long dateTimeMillis = cursorA.getLong(cursorA.getColumnIndex(A.d));
final String selectionB = B.p + SQL_LIKE;
final String[] selectionArgsB = new String[]{'%' + phone};
final Cursor cursorB = getContentResolver().query(URI_A, null,
selectionB, selectionArgsB, A.d + " DESC LIMIT 1");
if(cursorB != null && cursorB.moveToFirst()){
final EntityB entityB = new EntityB().getUnitFromCursor(cursorB);
final ContentValues contentValues = new ContentValues();
contentValues.put(B.d, dateTimeMillis);
contentValues.put(B.u, durationMillis);
final String where = B._ID + SQL_ADD_VALUE;
final String[] whereArgs = new String[]{entityB.getId()};
getContentResolver.update(URI_B, values, where, whereArgs)
cursorB.close();
}
callLogCursor.close();
}
问:我怎样才能提高我的代码,以便在将来如果我需要的东西一样,我不需要写这么多的样板,它看起来更干净。我确信这段代码可以在单个SQL语句中更改 - 我认为这是解决此类问题的最佳解决方案。
你想标记除SQLite之外的其他语言吗? – Yunnosch
请制作[mcve],由一些“create ...”行和几行“insert ...”组成。 SQLite命令行工具具有'.dump'功能,可以方便地进行操作。为您的mcve的示例内容添加所需的输出。 – Yunnosch
如果你的代码基本上是做你想做的事情,那么这个问题需要一个特定的问题描述。否则,由于太宽泛或属于不同的网站而不是主题,即https://codereview.stackexchange.com/ – Yunnosch