use of android.database.sqlite.SQLiteDatabase in project tray by grandcentrix.
the class TrayDBHelperTest method assertV2Integrity.
private void assertV2Integrity(final TrayDBHelper trayDBHelper) {
final SQLiteDatabase db = trayDBHelper.getReadableDatabase();
{
// check added MIGRATED_KEY column
final Cursor cursor = db.query(TrayDBHelper.TABLE_NAME, null, null, null, null, null, null);
assertNotNull(cursor);
final List<String> columnNames = Arrays.asList(cursor.getColumnNames());
cursor.close();
assertEquals(7, columnNames.size());
assertTrue(columnNames.contains(BaseColumns._ID));
assertTrue(columnNames.contains(TrayDBHelper.MODULE));
assertTrue(columnNames.contains(TrayDBHelper.KEY));
assertTrue(columnNames.contains(TrayDBHelper.VALUE));
assertTrue(columnNames.contains(TrayDBHelper.CREATED));
assertTrue(columnNames.contains(TrayDBHelper.UPDATED));
assertTrue(columnNames.contains(TrayDBHelper.MIGRATED_KEY));
}
{
// check added internal tray table
final Cursor cursor = db.query(TrayDBHelper.INTERNAL_TABLE_NAME, null, null, null, null, null, null);
assertNotNull(cursor);
final List<String> columnNames = Arrays.asList(cursor.getColumnNames());
cursor.close();
assertEquals(7, columnNames.size());
assertTrue(columnNames.contains(BaseColumns._ID));
assertTrue(columnNames.contains(TrayDBHelper.MODULE));
assertTrue(columnNames.contains(TrayDBHelper.KEY));
assertTrue(columnNames.contains(TrayDBHelper.VALUE));
assertTrue(columnNames.contains(TrayDBHelper.CREATED));
assertTrue(columnNames.contains(TrayDBHelper.UPDATED));
assertTrue(columnNames.contains(TrayDBHelper.MIGRATED_KEY));
}
db.close();
}
use of android.database.sqlite.SQLiteDatabase in project tray by grandcentrix.
the class TrayDBHelperTest method assertV1Integrity.
private void assertV1Integrity(final TrayDBHelper trayDBHelper) {
final SQLiteDatabase db = trayDBHelper.getReadableDatabase();
{
// check tray table
final Cursor cursor = db.query(TrayDBHelper.TABLE_NAME, null, null, null, null, null, null);
assertNotNull(cursor);
final List<String> columnNames = Arrays.asList(cursor.getColumnNames());
cursor.close();
assertEquals(6, columnNames.size());
assertTrue(columnNames.contains(BaseColumns._ID));
assertTrue(columnNames.contains(TrayDBHelper.MODULE));
assertTrue(columnNames.contains(TrayDBHelper.KEY));
assertTrue(columnNames.contains(TrayDBHelper.VALUE));
assertTrue(columnNames.contains(TrayDBHelper.CREATED));
assertTrue(columnNames.contains(TrayDBHelper.UPDATED));
}
db.close();
}
use of android.database.sqlite.SQLiteDatabase in project ListenerMusicPlayer by hefuyicoder.
the class RecentStore method addSongId.
/**
* 将新播放曲目id插入表中,并保持总记录数在100条
* @param songId 新播放曲目的ID
*/
public void addSongId(final long songId) {
final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
database.beginTransaction();
try {
Cursor mostRecentItem = null;
try {
mostRecentItem = queryRecentIds("1");
if (mostRecentItem != null && mostRecentItem.moveToFirst()) {
if (songId == mostRecentItem.getLong(0)) {
return;
}
}
} finally {
if (mostRecentItem != null) {
mostRecentItem.close();
mostRecentItem = null;
}
}
final ContentValues values = new ContentValues(2);
values.put(RecentStoreColumns.ID, songId);
values.put(RecentStoreColumns.TIMEPLAYED, System.currentTimeMillis());
database.insert(RecentStoreColumns.NAME, null, values);
Cursor oldest = null;
try {
oldest = database.query(RecentStoreColumns.NAME, new String[] { RecentStoreColumns.TIMEPLAYED }, null, null, null, null, RecentStoreColumns.TIMEPLAYED + " ASC");
if (oldest != null && oldest.getCount() > MAX_ITEMS_IN_DB) {
oldest.moveToPosition(oldest.getCount() - MAX_ITEMS_IN_DB);
long timeOfRecordToKeep = oldest.getLong(0);
database.delete(RecentStoreColumns.NAME, RecentStoreColumns.TIMEPLAYED + " < ?", new String[] { String.valueOf(timeOfRecordToKeep) });
}
} finally {
if (oldest != null) {
oldest.close();
oldest = null;
}
}
} finally {
database.setTransactionSuccessful();
database.endTransaction();
}
}
use of android.database.sqlite.SQLiteDatabase in project ListenerMusicPlayer by hefuyicoder.
the class SongPlayCount method bumpSongCount.
/**
* Increases the play count of a song by 1
*
* @param songId The song id to increase the play count
*/
public void bumpSongCount(final long songId) {
if (songId < 0) {
return;
}
final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
updateExistingRow(database, songId, true);
}
use of android.database.sqlite.SQLiteDatabase in project ListenerMusicPlayer by hefuyicoder.
the class FavoriteSong method addFavoriteSong.
public int addFavoriteSong(final long[] songId) {
final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
database.beginTransaction();
Cursor cursor = null;
int insert = 0;
try {
for (long aSongId : songId) {
cursor = database.query(FavoriteSongColumns.NAME, new String[] { FavoriteSongColumns.SONGID }, FavoriteSongColumns.SONGID + " =? ", new String[] { String.valueOf(songId) }, null, null, null);
if (cursor != null && cursor.getCount() == 0) {
//若无重复则插入
ContentValues values = new ContentValues(2);
values.put(FavoriteSongColumns.SONGID, aSongId);
values.put(FavoriteSongColumns.TIMEADDED, System.currentTimeMillis());
database.insert(FavoriteSongColumns.NAME, null, values);
insert++;
}
}
return insert;
} finally {
if (cursor != null) {
cursor.close();
}
database.setTransactionSuccessful();
database.endTransaction();
}
}
Aggregations