Android SQLiteDatabase - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android SQLiteDatabase class.

This code example shows how to use the following methods:execSQL
        super(context, NAME, null, VERSION);
    } 
 
    @Override 
    public void onCreate(final SQLiteDatabase db) {
        db.execSQL("CREATE TABLE orgs (id INTEGER PRIMARY KEY);");
        db.execSQL("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, avatarurl TEXT);");
        db.execSQL("CREATE TABLE repos (id INTEGER PRIMARY KEY, repoId INTEGER, orgId INTEGER, name TEXT, ownerId INTEGER, private INTEGER, fork INTEGER, description TEXT, forks INTEGER, watchers INTEGER, language TEXT, hasIssues INTEGER, mirrorUrl TEXT);");
    } 
 
    @Override 
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
            final int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS orgs");
        db.execSQL("DROP TABLE IF EXISTS users");
        db.execSQL("DROP TABLE IF EXISTS repos");
        onCreate(db);
    } 
} 
10
CodeRank
This code example shows how to use the following methods:beginTransaction, endTransaction, setTransactionSuccessful
 
        final SQLiteDatabase db = getWritable(helper);
        if (db == null)
            return items;
 
        db.beginTransaction();
        try { 
            persistableResource.store(db, items);
            db.setTransactionSuccessful();
        } finally { 
            db.endTransaction();
        } 
        return items;
    } 
 
    private <E> List<E> loadFromDB(final SQLiteOpenHelper helper,
            final PersistableResource<E> persistableResource) {
        final SQLiteDatabase db = getReadable(helper);
        if (db == null)
            return null; 
9
CodeRank
This code example shows how to use the following methods:close
 
    /** Closes database resource */ 
    @Override 
    public void close() { 
        if(syncDatabase != null)
            syncDatabase.close();
    } 
} 
8
CodeRank
This code example shows how to use the following methods:beginTransaction, endTransaction, setTransactionSuccessful, update
    @Override 
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = null;
        try { 
            db = mDatabaseHelper.getWritableDatabase();
            db.beginTransaction();
 
            String newSelection = buildSelection(uri, selection);
            int count = db.update("tests", values, newSelection, selectionArgs);
            db.setTransactionSuccessful();
            return count;
        } finally { 
            if (db != null) {
                db.endTransaction();
            } 
        } 
    } 
 
    private String buildSelection(Uri uri, String selection) {
        long id = 0;
7
CodeRank
This code example shows how to use the following methods:execSQL, setVersion
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.append(String.format("CREATE TABLE %s (", TABLE_NAME));
            TrackerEntry.buildCreationString(queryBuilder);
 
            queryBuilder.append(");");
            db.execSQL(queryBuilder.toString());
            db.setVersion(DB_VERSION);
        } 
 
        @Override 
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO: reimplement this when dB version changes 
            Log.w(LOG_TAG, "Upgrading database from version " + oldVersion
                            + " to " + newVersion
                            + ", which will destroy all old data"); 
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        } 
    } 
 
    private DatabaseHelper mOpenHelper;
8
CodeRank
This code example shows how to use the following methods:execSQL, insert
		values.put("Search_Proxy_Url", account.getSearchProxyUrl());
		values.put("Token_Expired_At",
				account.getTokenExpiredAt() == null? -1L : account.getTokenExpiredAt().getTime());
 
		if (account.getUser() != null) {
			userDao.save(sqLiteDatabase, account.getUser());
		} 
		 
		if (account.isDefault()) {
			resetDefaultAccount(sqLiteDatabase);  // 清除原有的默认帐号状态设置
		} 
		 
		clearDeletedAccount(sqLiteDatabase, account); // 确保一个帐号在数据库中只有一条记录,一个状态
 
		long rowId = sqLiteDatabase.insert(TABLE, null, values);
		//You can access the ROWID of an SQLite table using one the special 
		//column names ROWID, _ROWID_, or OID. 
		//If a table contains a column of type INTEGER PRIMARY KEY, 
		//then that column becomes an alias for the ROWID. 
		//You can then access the ROWID using any of four different names, 

8
CodeRank
This code example shows how to use the following methods:execSQL
This code example shows how to use the following methods:close, delete, insert, query
		if(fileName != null)
			mActivity.deleteFile(fileName);
		//mActivity.openFileOutput(fileName, Context.MODE_PRIVATE).getFD(). 
 
		String[] whereArgs = {playlistName};
		db.delete(TABLE_PLAYLIST, "PlaylistName = ?", whereArgs);
 
		db.close();
	} 
 
	@Override 
	public ArrayList<String> getAvailablePlaylists() {
		ArrayList<String> playlists = new ArrayList<String>(); 
		SQLiteDatabase db = mActivity.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
 
		String[] columns = {"PlaylistName"};
		Cursor query = db.query(TABLE_PLAYLIST, columns, null, null, null, null, "PlaylistName ASC");
 
		if(query != null){
			query.moveToFirst();

6
CodeRank
This code example shows how to use the following methods:execSQL
		super(context, DB_NAME, null, DB_VERSION);
	} 
	 
	@Override 
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_BOOKMARKS_TABLE);
		db.execSQL(CREATE_TAGS_TABLE);
		db.execSQL(CREATE_BOOKMARK_TAG_TABLE);
      db.execSQL(BOOKMARK_TAGS_INDEX);
	} 
 
	@Override 
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	   if (newVersion <= oldVersion) {
	      Log.w(TAG, "Can't downgrade from version " +
                 oldVersion + " to version " + newVersion);
	      return; 
	   } 
      Log.i(TAG, "Upgrading database from version " +
              oldVersion + " to version " + newVersion);
7
CodeRank
This code example shows how to use the following methods:beginTransaction, delete, endTransaction, setTransactionSuccessful
            file = new File(getLockPatternFilename(userId));
            if (file.exists()) {
                file.delete();
            } 
 
            db.beginTransaction();
            db.delete(TABLE, COLUMN_USERID + "='" + userId + "'", null);
            db.setTransactionSuccessful();
        } finally { 
            db.endTransaction();
        } 
    } 
 
    private void writeFile(String name, byte[] hash) {
        try { 
            // Write the hash to file 
            RandomAccessFile raf = new RandomAccessFile(name, "rw");
            // Truncate the file if pattern is null, to clear the lock 
            if (hash == null || hash.length == 0) {
                raf.setLength(0);
7
CodeRank

Related Android SQLiteDatabase Questions & Answers:

Other Android SQLiteDatabase Examples

1:		public void closeDB() {
2:			SQLiteDatabase db = this.getReadableDatabase();
3:			if (db != null && db.isOpen())
4:				db.close();

Read More

2:		 */
3:		public void deleteToDo(long tado_id) {
4:			SQLiteDatabase db = this.getWritableDatabase();
5:			db.delete(TABLE_TODO, KEY_ID + " = ?",
6:					new String[] { String.valueOf(tado_id) });

Read More

1:		public void deleteContact(Contact contact) {
2:			SQLiteDatabase db = this.getWritableDatabase();
3:			db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
4:					new String[] { String.valueOf(contact.getID()) });

Read More

See Code Examples for Android SQLiteDatabase Methods: