DatabaseHandler.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.util.ArrayList;
  5. import de.tudarmstadt.informatik.hostage.R;
  6. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  7. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  8. import android.content.ContentValues;
  9. import android.content.Context;
  10. import android.database.Cursor;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13. import android.util.Log;
  14. /**
  15. * This class creates SQL tables and handles all access to the database.<br>
  16. * It contains several methods with predefined queries to extract different kinds of information from the database.<br>
  17. * The database contains two tables: {@link #TABLE_RECORDS} and {@link #TABLE_BSSIDS}:<br>
  18. * {@link #TABLE_RECORDS} contains all logging information of a single message record except the SSID.<br>
  19. * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the corresponding SSID.<br>
  20. * @author Lars Pandikow
  21. */
  22. public class DatabaseHandler extends SQLiteOpenHelper {
  23. // All Static variables
  24. // Database Version
  25. private static final int DATABASE_VERSION = 1;
  26. // Database Name
  27. private static final String DATABASE_NAME = "recordManager";
  28. // Contacts table names
  29. private static final String TABLE_ATTACK_INFO = "attack_info";
  30. private static final String TABLE_RECORDS = "records";
  31. private static final String TABLE_BSSIDS = "bssids";
  32. private static final String TABLE_PORTS = "ports";
  33. // Contacts Table Columns names
  34. private static final String KEY_ID = "_id";
  35. private static final String KEY_ATTACK_ID = "_attack_id";
  36. private static final String KEY_TYPE = "type";
  37. private static final String KEY_TIME = "timestamp";
  38. private static final String KEY_PACKET = "packet";
  39. private static final String KEY_PROTOCOL = "protocol";
  40. private static final String KEY_EXTERNAL_IP ="externalIP";
  41. private static final String KEY_LOCAL_IP = "localIP";
  42. private static final String KEY_LOCAL_HOSTNAME = "localHostName";
  43. private static final String KEY_LOCAL_PORT = "localPort";
  44. private static final String KEY_REMOTE_IP = "remoteIP";
  45. private static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
  46. private static final String KEY_REMOTE_PORT = "remotePort";
  47. private static final String KEY_BSSID = "_bssid";
  48. private static final String KEY_SSID = "ssid";
  49. private static final String KEY_LATITUDE = "latitude";
  50. private static final String KEY_LONGITUDE = "longitude";
  51. private static final String KEY_ACCURACY = "accuracy";
  52. // Database sql create statements
  53. private static final String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "("
  54. + KEY_ID + " INTEGER NOT NULL," + KEY_ATTACK_ID + " INTEGER NOT NULL,"
  55. + KEY_TYPE + " TEXT," + KEY_TIME + " INTEGER," + KEY_PACKET + " TEXT,"
  56. + "FOREIGN KEY("+ KEY_ATTACK_ID +") REFERENCES " + TABLE_ATTACK_INFO + "("+KEY_ATTACK_ID+")"
  57. + "PRIMARY KEY("+ KEY_ID + ", " + KEY_ATTACK_ID + ")"
  58. + ")";
  59. private static final String CREATE_ATTACK_INFO_TABLE = "CREATE TABLE " + TABLE_ATTACK_INFO + "("
  60. + KEY_ATTACK_ID + " INTEGER PRIMARY KEY," + KEY_PROTOCOL + " TEXT,"
  61. + KEY_EXTERNAL_IP + " TEXT," + KEY_LOCAL_IP + " BLOB," + KEY_LOCAL_HOSTNAME + " TEXT,"
  62. + KEY_REMOTE_IP + " BLOB," + KEY_REMOTE_HOSTNAME + " TEXT," + KEY_REMOTE_PORT + " INTEGER," + KEY_BSSID + " TEXT,"
  63. + "FOREIGN KEY("+ KEY_BSSID +") REFERENCES " + TABLE_BSSIDS + "("+KEY_BSSID+")"
  64. + "FOREIGN KEY("+ KEY_PROTOCOL +") REFERENCES " + TABLE_PORTS + "("+KEY_PROTOCOL+")"
  65. + ")";
  66. private static final String CREATE_BSSID_TABLE = "CREATE TABLE " + TABLE_BSSIDS + "("
  67. + KEY_BSSID + " TEXT PRIMARY KEY," + KEY_SSID + " TEXT," + KEY_LATITUDE + " INTEGER,"
  68. + KEY_LONGITUDE + " INTEGER," + KEY_ACCURACY + " INTEGER"
  69. + ")";
  70. private static final String CREATE_PORT_TABLE = "CREATE TABLE " + TABLE_PORTS + "("
  71. + KEY_PROTOCOL + " TEXT PRIMARY KEY," + KEY_LOCAL_PORT + " INTEGER"
  72. + ")";
  73. private Context context;
  74. public DatabaseHandler(Context context) {
  75. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  76. this.context = context;
  77. }
  78. // Creating Tables
  79. @Override
  80. public void onCreate(SQLiteDatabase db) {
  81. db.execSQL(CREATE_PORT_TABLE);
  82. db.execSQL(CREATE_BSSID_TABLE);
  83. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  84. db.execSQL(CREATE_RECORD_TABLE);
  85. String[] protocols = context.getResources().getStringArray(R.array.protocols);
  86. String packageName = Protocol.class.getPackage().getName();
  87. //Initialize Port Table
  88. for (String protocol : protocols) {
  89. try {
  90. int port = ((Protocol<?>) Class.forName(String.format("%s.%s", packageName, protocol)).newInstance()).getPort();
  91. db.execSQL("INSERT INTO " + TABLE_PORTS + " VALUES ( '" + protocol + "'," + port + ")");
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. }
  97. // Upgrading database
  98. @Override
  99. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  100. // Drop older table if existed
  101. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  102. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  103. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  104. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PORTS);
  105. // Create tables again
  106. onCreate(db);
  107. }
  108. /**
  109. * Adds a given {@link Record} to the database.
  110. * @param record The added {@link Record} .
  111. */
  112. public void addRecord(Record record) {
  113. SQLiteDatabase db = this.getWritableDatabase();
  114. ContentValues bssidValues = new ContentValues();
  115. bssidValues.put(KEY_BSSID, record.getBSSID());
  116. bssidValues.put(KEY_SSID, record.getSSID());
  117. bssidValues.put(KEY_LATITUDE, record.getLatitude());
  118. bssidValues.put(KEY_LONGITUDE, record.getLongitude());
  119. bssidValues.put(KEY_ACCURACY, record.getAccuracy());
  120. ContentValues attackValues = new ContentValues();
  121. attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  122. attackValues.put(KEY_PROTOCOL, record.getProtocol().toString());
  123. attackValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
  124. attackValues.put(KEY_LOCAL_IP, record.getLocalIP().getAddress()); // Log Local IP
  125. attackValues.put(KEY_LOCAL_HOSTNAME, record.getLocalIP().getHostName());
  126. attackValues.put(KEY_REMOTE_IP, record.getRemoteIP().getAddress()); // Log Remote IP
  127. attackValues.put(KEY_REMOTE_HOSTNAME, record.getRemoteIP().getHostName());
  128. attackValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  129. attackValues.put(KEY_BSSID, record.getBSSID());
  130. ContentValues recordValues = new ContentValues();
  131. recordValues.put(KEY_ID, record.getId()); // Log Message Number
  132. recordValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  133. recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
  134. recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
  135. recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
  136. // Inserting Rows
  137. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  138. db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  139. db.insert(TABLE_RECORDS, null, recordValues);
  140. db.close(); // Closing database connection
  141. }
  142. /**
  143. * Creates a {@link Record} from a Cursor. If the cursor does not show to a valid data structure a runtime exception is thrown.
  144. * @param cursor
  145. * @return Returns the created {@link Record} .
  146. */
  147. private Record createRecord(Cursor cursor){
  148. Record record = new Record();
  149. try {
  150. record.setId(Integer.parseInt(cursor.getString(0)));
  151. record.setAttack_id(cursor.getLong(1));
  152. record.setType(cursor.getString(2).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
  153. record.setTimestamp(cursor.getLong(3));
  154. record.setPacket(cursor.getString(4));
  155. record.setProtocol(cursor.getString(5));
  156. record.setExternalIP(cursor.getString(6));
  157. record.setLocalIP(InetAddress.getByAddress(cursor.getString(8), cursor.getBlob(7)));
  158. record.setRemoteIP(InetAddress.getByAddress(cursor.getString(10), cursor.getBlob(9)));
  159. record.setRemotePort(Integer.parseInt(cursor.getString(11)));
  160. record.setBSSID(cursor.getString(12));
  161. record.setSSID(cursor.getString(13));
  162. record.setLatitude(Double.parseDouble(cursor.getString(14)));
  163. record.setLongitude(Double.parseDouble(cursor.getString(15)));
  164. record.setAccuracy(Float.parseFloat(cursor.getString(16)));
  165. record.setLocalPort(Integer.parseInt(cursor.getString(17)));
  166. } catch (UnknownHostException e) {
  167. e.printStackTrace();
  168. }
  169. return record;
  170. }
  171. /**
  172. * Gets a single {@link Record} with the given ID from the database.
  173. * @param id The ID of the {@link Record};
  174. * @return The {@link Record}.
  175. */
  176. public Record getRecord(int id) {
  177. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ID + " = " + id;
  178. SQLiteDatabase db = this.getReadableDatabase();
  179. Cursor cursor = db.rawQuery(selectQuery, null);
  180. Record record = null;
  181. if (cursor.moveToFirst()){
  182. record = createRecord(cursor);
  183. }
  184. cursor.close();
  185. db.close();
  186. // return contact
  187. return record;
  188. }
  189. /**
  190. * Gets all {@link Record Records} saved in the database.
  191. * @return A ArrayList of all the {@link Record Records} in the Database.
  192. */
  193. public ArrayList<Record> getAllRecords() {
  194. ArrayList<Record> recordList = new ArrayList<Record>();
  195. // Select All Query
  196. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  197. SQLiteDatabase db = this.getWritableDatabase();
  198. Cursor cursor = db.rawQuery(selectQuery, null);
  199. Log.i("Database", "Start loop");
  200. // looping through all rows and adding to list
  201. if (cursor.moveToFirst()) {
  202. do {
  203. Log.i("Database", "Add Record");
  204. Record record = createRecord(cursor);
  205. // Adding record to list
  206. recordList.add(record);
  207. } while (cursor.moveToNext());
  208. }
  209. cursor.close();
  210. db.close();
  211. // return record list
  212. return recordList;
  213. }
  214. /**
  215. * Gets a single {@link Record} with the given attack id from the database.
  216. * @param attack_id The attack id of the {@link Record};
  217. * @return The {@link Record}.
  218. */
  219. public Record getRecordOfAttackId(long attack_id) {
  220. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
  221. SQLiteDatabase db = this.getReadableDatabase();
  222. Cursor cursor = db.rawQuery(selectQuery, null);
  223. Record record = null;
  224. if (cursor.moveToFirst()) {
  225. record = createRecord(cursor);
  226. }
  227. cursor.close();
  228. // return record list
  229. db.close();
  230. return record;
  231. }
  232. /**
  233. * Gets all received {@link Record Records} for every attack identified by its attack id and ordered by date.
  234. * @return A ArrayList with all received {@link Record Records} for each attack id in the Database.
  235. */
  236. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  237. ArrayList<Record> recordList = new ArrayList<Record>();
  238. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_TYPE + "='RECEIVE'" + " ORDER BY " + KEY_TIME;
  239. SQLiteDatabase db = this.getReadableDatabase();
  240. Cursor cursor = db.rawQuery(selectQuery, null);
  241. // looping through all rows and adding to list
  242. if (cursor.moveToFirst()) {
  243. do {
  244. Record record = createRecord(cursor);
  245. // Adding record to list
  246. recordList.add(record);
  247. } while (cursor.moveToNext());
  248. }
  249. cursor.close();
  250. // return record list
  251. db.close();
  252. return recordList;
  253. }
  254. /**
  255. * Gets a representative {@link Record} for every attack identified by its attack id.
  256. * @return A ArrayList with one {@link Record Records} for each attack id in the Database.
  257. */
  258. public ArrayList<Record> getRecordOfEachAttack() {
  259. ArrayList<Record> recordList = new ArrayList<Record>();
  260. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " GROUP BY " + KEY_ATTACK_ID;
  261. SQLiteDatabase db = this.getReadableDatabase();
  262. Cursor cursor = db.rawQuery(selectQuery, null);
  263. // looping through all rows and adding to list
  264. if (cursor.moveToFirst()) {
  265. do {
  266. Record record = createRecord(cursor);
  267. // Adding record to list
  268. recordList.add(record);
  269. } while (cursor.moveToNext());
  270. }
  271. cursor.close();
  272. // return record list
  273. db.close();
  274. return recordList;
  275. }
  276. /**
  277. * Gets a representative {@link Record} for every attack with a higher attack id than the specified.
  278. * @param attack_id The attack id to match the query against.
  279. * @return A ArrayList with one {@link Record Records} for each attack id higher than the given.
  280. */
  281. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  282. ArrayList<Record> recordList = new ArrayList<Record>();
  283. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
  284. SQLiteDatabase db = this.getReadableDatabase();
  285. Cursor cursor = db.rawQuery(selectQuery, null);
  286. // looping through all rows and adding to list
  287. if (cursor.moveToFirst()) {
  288. do {
  289. Record record = createRecord(cursor);
  290. // Adding record to list
  291. recordList.add(record);
  292. } while (cursor.moveToNext());
  293. }
  294. cursor.close();
  295. // return count
  296. db.close();
  297. return recordList;
  298. }
  299. /**
  300. * Determines the number of {@link Record Records} in the database.
  301. * @return The number of {@link Record Records} in the database.
  302. */
  303. public int getRecordCount() {
  304. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  305. SQLiteDatabase db = this.getReadableDatabase();
  306. Cursor cursor = db.rawQuery(countQuery, null);
  307. int result = cursor.getCount();
  308. cursor.close();
  309. // return count
  310. db.close();
  311. return result;
  312. }
  313. /**
  314. * Determines the number of different attack_ids in the database.
  315. * @return The number of different attack_ids in the database.
  316. */
  317. public int getAttackCount() {
  318. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  319. SQLiteDatabase db = this.getReadableDatabase();
  320. Cursor cursor = db.rawQuery(countQuery, null);
  321. int result = cursor.getCount();
  322. cursor.close();
  323. // return count
  324. db.close();
  325. return result;
  326. }
  327. /**
  328. * Determines the number of different attack_ids for a specific protocol in the database.
  329. * @param protocol The String representation of the {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol}
  330. * @return The number of different attack_ids in the database.
  331. */
  332. public int getAttackPerProtokolCount(String protocol) {
  333. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  334. SQLiteDatabase db = this.getReadableDatabase();
  335. Cursor cursor = db.rawQuery(countQuery, null);
  336. int result = cursor.getCount();
  337. cursor.close();
  338. // return count
  339. db.close();
  340. return result;
  341. }
  342. /**
  343. * Determines the smallest attack id stored in the database.
  344. * @return The smallest attack id stored in the database.
  345. */
  346. public long getSmallestAttackId(){
  347. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID +") FROM " + TABLE_ATTACK_INFO;
  348. SQLiteDatabase db = this.getReadableDatabase();
  349. Cursor cursor = db.rawQuery(selectQuery, null);
  350. int result;
  351. if (cursor.moveToFirst()) {
  352. result = cursor.getInt(0);
  353. } else{
  354. result = -1;
  355. }
  356. cursor.close();
  357. db.close();
  358. return result;
  359. }
  360. /**
  361. * Determines the highest attack id stored in the database.
  362. * @return The highest attack id stored in the database.
  363. */
  364. public long getHighestAttackId(){
  365. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID +") FROM " + TABLE_ATTACK_INFO;
  366. SQLiteDatabase db = this.getReadableDatabase();
  367. Cursor cursor = db.rawQuery(selectQuery, null);
  368. int result;
  369. if (cursor.moveToFirst()) {
  370. result = cursor.getInt(0);
  371. } else{
  372. result = -1;
  373. }
  374. cursor.close();
  375. db.close();
  376. return result;
  377. }
  378. /**
  379. * Determines if an attack has been recorded on a specific protocol in a network with a given BSSID.
  380. * @param protocol The {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol} to inspect.
  381. * @param BSSID The BSSID of the network.
  382. * @return True if an attack on the given protocol has been recorded in a network with the given BSSID, else false.
  383. */
  384. public boolean bssidSeen(String protocol, String BSSID){
  385. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS+ " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = " + "'" + BSSID + "'";
  386. SQLiteDatabase db = this.getReadableDatabase();
  387. Cursor cursor = db.rawQuery(countQuery, null);
  388. int result = cursor.getCount();
  389. cursor.close();
  390. db.close();
  391. return result > 0;
  392. }
  393. /**
  394. * Returns a String array with all BSSIDs stored in the database.
  395. * @return String[] of all recorded BSSIDs.
  396. */
  397. public String[] getAllBSSIDS(){
  398. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  399. SQLiteDatabase db = this.getReadableDatabase();
  400. Cursor cursor = db.rawQuery(selectQuery, null);
  401. String[] bssidList = new String[cursor.getCount()];
  402. int counter = 0;
  403. // looping through all rows and adding to list
  404. if (cursor.moveToFirst()) {
  405. do {
  406. bssidList[counter] = cursor.getString(0);
  407. counter++;
  408. } while (cursor.moveToNext());
  409. }
  410. cursor.close();
  411. db.close();
  412. return bssidList;
  413. }
  414. /**
  415. * Gets the last recorded SSID to a given BSSID.
  416. * @param bssid The BSSID to match against.
  417. * @return A String of the last SSID or null if the BSSID is not in the database.
  418. */
  419. public String getSSID(String bssid){
  420. String selectQuery = "SELECT "+ KEY_SSID +" FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  421. SQLiteDatabase db = this.getReadableDatabase();
  422. Cursor cursor = db.rawQuery(selectQuery, null);
  423. String ssid = null;
  424. if(cursor.moveToFirst()){
  425. ssid = cursor.getString(0);
  426. }
  427. cursor.close();
  428. db.close();
  429. return ssid;
  430. }
  431. /**
  432. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  433. * @param bssid The BSSID to match against.
  434. */
  435. public void deleteByBSSID(String bssid){
  436. SQLiteDatabase db = this.getReadableDatabase();
  437. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[]{bssid});
  438. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[]{bssid});
  439. db.close();
  440. }
  441. //TODO Delete statement überarbeiten
  442. /**
  443. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller then the given
  444. * @param date A Date represented in milliseconds.
  445. */
  446. public void deleteByDate(long date){
  447. SQLiteDatabase db = this.getReadableDatabase();
  448. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
  449. //TODO Delete statement überarbeiten
  450. // String deleteQuery2 = "DELETE "
  451. db.execSQL(deleteQuery);
  452. db.close();
  453. }
  454. /**
  455. * Deletes all records from {@link #TABLE_RECORDS}.
  456. */
  457. public void clearData(){
  458. SQLiteDatabase db = this.getReadableDatabase();
  459. db.delete(TABLE_RECORDS, null, null);
  460. db.delete(TABLE_ATTACK_INFO, null, null);
  461. db.close();
  462. }
  463. }