Search in sources :

Example 76 with SQLiteDatabase

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();
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) List(java.util.List) Cursor(android.database.Cursor)

Example 77 with SQLiteDatabase

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();
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) List(java.util.List) Cursor(android.database.Cursor)

Example 78 with SQLiteDatabase

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();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Example 79 with SQLiteDatabase

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);
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 80 with SQLiteDatabase

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();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1658 Cursor (android.database.Cursor)527 ContentValues (android.content.ContentValues)350 ArrayList (java.util.ArrayList)111 File (java.io.File)65 Test (org.junit.Test)59 SQLiteException (android.database.sqlite.SQLiteException)48 SQLException (android.database.SQLException)44 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)44 Uri (android.net.Uri)44 IOException (java.io.IOException)43 ServiceStatus (com.vodafone360.people.service.ServiceStatus)42 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)38 RemoteException (android.os.RemoteException)36 Pair (android.util.Pair)31 MediumTest (android.test.suitebuilder.annotation.MediumTest)30 Account (android.accounts.Account)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)22 HashMap (java.util.HashMap)21