AggregatorRecordStoragepublic class AggregatorRecordStorage extends AggregatorStorage
Fields Summary |
---|
private static final String | TAG | private String | mTableName | private List | mColumnNames |
Constructors Summary |
---|
public AggregatorRecordStorage(android.content.Context context, String tableName, String[] columnNames)
if (columnNames.length < 1) {
throw new RuntimeException("No column keys");
}
mColumnNames = Arrays.asList(columnNames);
mTableName = tableName;
String tableCmd = "create table " + tableName + "( " + columnNames[0] +
" TEXT";
for (int i = 1; i < columnNames.length; ++i)
tableCmd = tableCmd + ", " + columnNames[i] + " TEXT";
tableCmd = tableCmd + ");";
Log.i(TAG, tableCmd);
try {
mDbHelper = new DBHelper(context, tableName, tableCmd);
mDatabase = mDbHelper.getWritableDatabase();
} catch (SQLException e) {
throw new RuntimeException("Can't open table: " + tableName);
}
|
Methods Summary |
---|
public boolean | addData(java.util.Map data)
ContentValues content = new ContentValues();
for (Map.Entry<String, String> item : data.entrySet()) {
content.put(item.getKey(), item.getValue());
}
long rowID =
mDatabase.insert(mTableName, null, content);
return rowID >= 0;
| public java.util.List | getAllData()
ArrayList<Map<String, String> > allData = new ArrayList<Map<String, String> >();
Cursor cursor = mDatabase.rawQuery("select * from " + mTableName + ";", null);
if (cursor.getCount() == 0) {
return allData;
}
cursor.moveToFirst();
do {
HashMap<String, String> oneRow = new HashMap<String, String>();
for (String column : mColumnNames) {
int columnIndex = cursor.getColumnIndex(column);
if (!cursor.isNull(columnIndex)) {
String value = cursor.getString(columnIndex);
oneRow.put(column, value);
}
}
allData.add(oneRow);
} while (cursor.moveToNext());
return allData;
| public int | removeAllData()
int nDeleteRows = mDatabase.delete(mTableName, "1", null);
Log.i(TAG, "Number of rows in table deleted: " + nDeleteRows);
return nDeleteRows;
|
|