Code example for SQLiteDatabase

Methods: execSQLrawQuery, close

0
	public static final String CREATE_BLACKLIST = "CREATE TABLE IF NOT EXISTS DROIDSNIFF_BLACKLIST "
			+ "(id integer primary key autoincrement, " + "domain varchar(100));"; 
 
	public static void initDB(Context c) {
		DBHelper.droidsniffDB = c.openOrCreateDatabase(DROIDSNIFF_DBNAME, Context.MODE_PRIVATE, null);
		droidsniffDB.execSQL(CREATE_PREFERENCES);
		droidsniffDB.execSQL(CREATE_BLACKLIST);
	} 
 
	public static boolean getGeneric(Context c) {
		initDB(c);
		Cursor cur = droidsniffDB.rawQuery("SELECT * FROM DROIDSNIFF_PREFERENCES WHERE name = 'generic';", new String[] {});
		if (cur.moveToNext()) {
			String s = cur.getString(cur.getColumnIndex("value"));
			cur.close();
			droidsniffDB.close();
			return Boolean.parseBoolean(s);
		} else { 
			cur.close();
			droidsniffDB.close();
			return false; 
		} 
	} 
 
	public static HashMap<String, Object> getBlacklist(Context c) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		initDB(c);
		Cursor cur = droidsniffDB.rawQuery("SELECT domain FROM DROIDSNIFF_BLACKLIST;", new String[] {});
 
		while (cur.moveToNext()) {
			String s = cur.getString(cur.getColumnIndex("domain"));
			map.put(s, null);
		} 
 
		cur.close();
		droidsniffDB.close();
		return map;
	} 
 
	public static void addBlacklistEntry(Context c, String name) {
		initDB(c);
		droidsniffDB.execSQL("INSERT INTO DROIDSNIFF_BLACKLIST (domain) VALUES (?);", new Object[] { name });
		droidsniffDB.close();
	} 
 
	public static void setGeneric(Context c, boolean b) {
		initDB(c);
		Cursor cur = droidsniffDB.rawQuery("SELECT count(id) as count FROM DROIDSNIFF_PREFERENCES where name = 'generic';",
				new String[] {});
		cur.moveToFirst();
		int count = (int) cur.getLong(cur.getColumnIndex("count"));
		if (count == 0) {
			droidsniffDB.execSQL("INSERT INTO DROIDSNIFF_PREFERENCES (name, value) values ('generic', ?);",
					new String[] { Boolean.toString(b) });
		} else { 
			droidsniffDB.execSQL("UPDATE DROIDSNIFF_PREFERENCES SET value=? WHERE name='generic';",
					new String[] { Boolean.toString(b) });
		} 
		droidsniffDB.close();
	} 
 
	public static void clearBlacklist(Context c) {
		initDB(c);
		droidsniffDB.execSQL("DELETE FROM DROIDSNIFF_BLACKLIST;", new Object[] {});