我正在努力制作非常简单的东西,但我得出的结论是我的代码看起来非常难看,我很确定有一些方法可以提高代码性能和清晰度。
我怎样才能在Android SQLITE中使用SQL语句?
我有两张桌子:A和B. 在表格列d,p,u中。在表B列d,p,u中。列具有相同的名称。
我需要在每个表中找到最后添加的行,其中列p是某个值。并使用表A中列d和u的值更新表B中的列d和u。
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语句中更改 - 我认为这是解决此类问题的最佳解决方案。
答案 0 :(得分:0)
UPDATE B
SET d = (SELECT d
FROM A
WHERE p LIKE ...
ORDER BY d DESC
LIMIT 1),
u = (SELECT u
FROM A
WHERE p LIKE ...
ORDER BY d DESC
LIMIT 1)
WHERE ID = (SELECT ID
FROM B
WHERE p LIKE ...
ORDER BY d DESC
LIMIT 1);
如果A
中的p
可能没有B
中特定EXISTS (SELECT * FROM A WHERE ...)
的行,则必须在外部WHERE子句(public class ExtractionService {
private int maxAmountOfThreads = 10;
private int maxAmountOfMessagesPerThread = 1000;
private int rowsReserved = 0;
private int messagesProcessed = 0;
private int messagesToProcess = 0; // value set later
private List<String> ids = new ArrayList<String>();
private List<Thread> threads = new ArrayList<Thread>();
public void selectMessages(selectionAttributes) {
final ExtractionService extractionService = this;
ids = dao.selectIds(selectionAttributes) // selectionAttributes holds the configuration to only select the ids of the rows that match the users filter
messagesToProcess = ids.size(); // can be for example 150'000
// If there are less rows returned than maxAmountOfThreads * maxAmountOfMessagesPerThread decrease the max amount of threads generated
if((messagesToProcess / maxAmountOfThreads) <= maxAmountOfMessagesPerThread ) {
maxAmountOfThreads = (int) (Math.ceil((float)messagesToProcess / (float)maxAmountOfMessagesPerThread ));
}
for(int i = 0; i < maxAmountOfThreads; i++) { //Create threads with start index (first messages to process for example starts at message 1001) and end index (last message to process in this thread for example 2001)
int startRowIndex = messagesProcessed + rowsReserved;
int lastRowIndex = ((startRowIndex + maxAmountOfMessagesPerThread < messagesToProcess) ? startRowIndex + maxAmountOfMessagesPerThread : messagesToProcess);
threadArray.add(i, new Thread(new TransactionExtraction_Thread(startRowIndex, lastRowIndex, extractionService)));
threadArray.get(i).start();
rowsReserved = rowsReserved + (lastRowIndex - startRowIndex);
}
boolean threadsAlive = true;
while (threadsAlive) {
threadsAlive = false;
for (int i = 0; i < threadArray.size(); i++) {
if (threadArray.get(i).isAlive()) {
threadsAlive = true;
break;
} else if(!threadArray.get(i).isAlive() && (messagesProcessed + rowsReserved) < messagesToProcess) {
int startRowIndex = messagesProcessed + rowsReserved;
int lastRowIndex = ((startRowIndex + maxAmountOfMessagesPerThread < messagesToProcess) ? startRowIndex + maxAmountOfMessagesPerThread : messagesToProcess);
threadArray.set(i, new Thread(new TransactionExtraction_Thread(startRowIndex, lastRowIndex, extractionService)));
threadArray.get(i).start();
rowsReserved = rowsReserved + (lastRowIndex - startRowIndex);
threadsAlive = true;
}
}
}
}
}
)中添加另一个过滤器