Code example for SQLiteDatabase

Methods: execSQL, longForQuery

0
     * 
     * @param id The _id of the event to duplicate. 
     */ 
    protected void duplicateEvent(final long id) {
        final SQLiteDatabase db = getWritableDatabase();
        final long canPartiallyUpdate = DatabaseUtils.longForQuery(db, "SELECT "
                + Calendars.CAN_PARTIALLY_UPDATE + " FROM " + Views.EVENTS
                + " WHERE " + Events._ID + " = ?", new String[] {
            String.valueOf(id)
        }); 
        if (canPartiallyUpdate == 0) {
            return; 
        } 
 
        db.execSQL("INSERT INTO " + CalendarDatabaseHelper.Tables.EVENTS
                + "  (" + LAST_SYNCED_EVENT_COLUMNS + ","
                +         Events.DIRTY + "," + Events.LAST_SYNCED + ")"
                + " SELECT " + LAST_SYNCED_EVENT_COLUMNS + ", 0, 1"
                + " FROM " + Tables.EVENTS
                + " WHERE "  + Events._ID + " = ? AND " + Events.DIRTY + " = ?",
                new Object[]{
                        id,
                        0, // Events.DIRTY 
                }); 
        final long newId = DatabaseUtils.longForQuery(
                db, "SELECT CASE changes() WHEN 0 THEN -1 ELSE last_insert_rowid() END", null);
        if (newId < 0) {
            return; 
        } 
 
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Duplicating event " + id + " into new event " + newId);
        } 
 
        copyEventRelatedTables(db, newId, id);
    } 
 
    /** 
     * Makes a copy of the Attendees, Reminders, and ExtendedProperties rows associated with 
     * a specific event. 
     * 
     * @param db The database. 
     * @param newId The ID of the new event. 
     * @param id The ID of the old event. 
     */ 
    static void copyEventRelatedTables(SQLiteDatabase db, long newId, long id) {
        db.execSQL("INSERT INTO " + Tables.REMINDERS
                + " ( "  + CalendarContract.Reminders.EVENT_ID + ", "
                        + LAST_SYNCED_REMINDER_COLUMNS + ") "
                + "SELECT ?," + LAST_SYNCED_REMINDER_COLUMNS
                + " FROM " + Tables.REMINDERS
                + " WHERE " + CalendarContract.Reminders.EVENT_ID + " = ?",
                new Object[] {newId, id});
        db.execSQL("INSERT INTO "
                + Tables.ATTENDEES
                + " (" + CalendarContract.Attendees.EVENT_ID + ","
                        + LAST_SYNCED_ATTENDEE_COLUMNS + ") "
                + "SELECT ?," + LAST_SYNCED_ATTENDEE_COLUMNS + " FROM " + Tables.ATTENDEES
                + " WHERE " + CalendarContract.Attendees.EVENT_ID + " = ?",
                new Object[] {newId, id});
        db.execSQL("INSERT INTO " + Tables.EXTENDED_PROPERTIES
                + " (" + CalendarContract.ExtendedProperties.EVENT_ID + ","
                + LAST_SYNCED_EXTENDED_PROPERTY_COLUMNS + ") "
                + "SELECT ?, " + LAST_SYNCED_EXTENDED_PROPERTY_COLUMNS
                + " FROM " + Tables.EXTENDED_PROPERTIES
                + " WHERE " + CalendarContract.ExtendedProperties.EVENT_ID + " = ?",
3
CodeRank
SQLiteDatabase.execSQL("INSERT INTO ExtendedProperties (event_id,name,value) SELECT ?, name,value FROM ExtendedProperties WHERE event_id = ?",Object[])Startstatic DatabaseUtils.longForQuery(SQLiteDatabase,"SELECT canPartiallyUpdate FROM view_events WHERE _id = ?",String[])SQLiteDatabase.execSQL("INSERT INTO Events  (_sync_id,calendar_id,title,eventLocation,description,eventColor,eventColor_index,eventStatus,selfAttendeeStatus,dtstart,dtend,eventTimezone,eventEndTimezone,duration,allDay,accessLevel,availability,hasAlarm,hasExtendedProperties,rrule,rdate,exrule,exdate,original_sync_id,original_id,originalInstanceTime,originalAllDay,lastDate,hasAttendeeData,guestsCanModify,guestsCanInviteOthers,guestsCanSeeGuests,organizer,isOrganizer,customAppPackage,customAppUri,uid2445,dirty,lastSynced) SELECT _sync_id,calendar_id,title,eventLocation,description,eventColor,eventColor_index,eventStatus,selfAttendeeStatus,dtstart,dtend,eventTimezone,eventEndTimezone,duration,allDay,accessLevel,availability,hasAlarm,hasExtendedProperties,rrule,rdate,exrule,exdate,original_sync_id,original_id,originalInstanceTime,originalAllDay,lastDate,hasAttendeeData,guestsCanModify,guestsCanInviteOthers,guestsCanSeeGuests,organizer,isOrganizer,customAppPackage,customAppUri,uid2445, 0, 1 FROM Events WHERE _id = ? AND dirty = ?",Object[])SQLiteDatabase.execSQL("INSERT INTO Attendees (event_id,attendeeName,attendeeEmail,attendeeStatus,attendeeRelationship,attendeeType,attendeeIdentity,attendeeIdNamespace) SELECT ?,attendeeName,attendeeEmail,attendeeStatus,attendeeRelationship,attendeeType,attendeeIdentity,attendeeIdNamespace FROM Attendees WHERE event_id = ?",Object[])SQLiteDatabase.execSQL("INSERT INTO Reminders ( event_id, minutes,method) SELECT ?,minutes,method FROM Reminders WHERE event_id = ?",Object[])static DatabaseUtils.longForQuery(SQLiteDatabase,"SELECT CASE changes() WHEN 0 THEN -1 ELSE last_insert_rowid() END",null)