Code example for SQLiteDatabase

Methods: insertinsertOrThrowqueryupdate

0
 
        switch (sUriMatcher.match(uri)) {
        case APPS:
            // TODO: Check validity of incoming data before inserting it 
            try { 
                rowId = mDb.insertOrThrow(Apps.TABLE_NAME, null, values);
            } catch (SQLException e) {
                String where = Apps.UID + "=? AND " + Apps.EXEC_UID + "=? AND "
                        + Apps.EXEC_CMD + "=?";
                String[] whereArgs = new String[] { values.getAsString(Apps.UID),
                        values.getAsString(Apps.EXEC_UID),
                        values.getAsString(Apps.EXEC_CMD)};
                mDb.update(Apps.TABLE_NAME, values,
                        where,
                        whereArgs);
                Cursor c = mDb.query(Apps.TABLE_NAME, new String[] { Apps._ID },
                        where,
                        whereArgs,
                        null, null, null); 
                if (c.moveToFirst()) {
                    rowId = c.getLong(0);
                } 
                c.close();
            } 
 
            if (values.getAsInteger(Apps.ALLOW) != Apps.AllowType.ASK) {
                ContentValues logValues = new ContentValues();
                logValues.put(Logs.APP_ID, rowId);
                logValues.put(Logs.DATE, System.currentTimeMillis());
                logValues.put(Logs.TYPE, Logs.LogType.CREATE);
                mDb.insert(Logs.TABLE_NAME, null, logValues);
                 
                Util.writeStoreFile(mContext,
                        values.getAsInteger(Apps.UID),
                        values.getAsInteger(Apps.EXEC_UID),
                        values.getAsString(Apps.EXEC_CMD),
                        values.getAsInteger(Apps.ALLOW));
            } 
            returnUri = ContentUris.withAppendedId(Apps.CONTENT_URI, rowId);
 
            break; 
        case APP_ID_LOGS:
        case LOGS_APP_ID:
            // TODO: Check validity of incoming data before inserting it 
            values.put(Logs.APP_ID, uri.getPathSegments().get(1));
            rowId = mDb.insert(Logs.TABLE_NAME, null, values);
            returnUri = ContentUris.withAppendedId(Logs.CONTENT_URI, rowId);
            // Logs are special, they should also notify of a change to the uri 
            // logs/app_id, and all apps uri 
            getContext().getContentResolver().notifyChange(
                    ContentUris.withAppendedId(Logs.CONTENT_URI,