我正在将电话的通话记录收集到表格中。
这很好用:
public long populate_Calls(String name, String phone, String type, String duration, String date, String contactid) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_PHONE, phone);
cv.put(KEY_TYPE, type);
cv.put(KEY_DURATION, duration);
cv.put(KEY_DATE, date);
cv.put(KEY_CONTACTID, contactid);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
假设该表包含 455 条记录。现在用户进行了一些调用,并刷新了表格。我只想将新的呼叫数据添加到表中。我一直在尝试使用这段代码(虽然我不太明白)但是每次刷新表格时,都会添加越来越多的记录。在模拟器中它工作正常(我只有 12 个拨出电话)。
我想我只需要查看电话号码和日期,因为它可以识别呼叫。如果具有该编号且在该日期的记录已经存在,则只需跳转到下一条记录。如果不存在,请将其添加到表中。正确的?
public void populate_Calls2(String name, String phone, String type, String duration, String date, String contactid) {
ourDatabase.execSQL("INSERT INTO " + DATABASE_TABLE + " (" + KEY_NAME + "," + KEY_PHONE + "," + KEY_TYPE + "," + KEY_DURATION + "," + KEY_DATE + "," + KEY_CONTACTID + ") SELECT * FROM (SELECT '" + name + "'," + phone + "," + type + "," + duration + "," + date + "," + contactid + ") WHERE NOT EXISTS (SELECT * FROM " + DATABASE_TABLE + " WHERE " + KEY_PHONE + " = '" + phone + "' AND " + KEY_DATE + " = '" + date + "') LIMIT 1;");
}
我也试过这个,但我明白了
准备'INSERT INTO CallTable VALUES(name,phone,type,duration,date,contactid) SELECT * FROM CallTable WHERE NOT EXISTS (SELECT * FROM CallTable WHERE phone = '30411552 ' AND 日期 = '1338569074976') 限制 1;'。
和语法错误:
ourDatabase.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES(" + KEY_NAME + "," + KEY_PHONE + "," + KEY_TYPE + "," + KEY_DURATION + "," + KEY_DATE + "," + KEY_CONTACTID + ") SELECT * FROM " + DATABASE_TABLE + " WHERE NOT EXISTS (SELECT * FROM " + DATABASE_TABLE + " WHERE " + KEY_PHONE + " = '" + phone + "' AND " + KEY_DATE + " = '" + date + "') LIMIT 1;");