Example usage for android.database.sqlite SQLiteDatabase beginTransaction

List of usage examples for android.database.sqlite SQLiteDatabase beginTransaction

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase beginTransaction.

Prototype

public void beginTransaction() 

Source Link

Document

Begins a transaction in EXCLUSIVE mode.

Usage

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void writeHighlight(Context context, Lecture lecture) {
    HighlightDBOpenHelper highlightDB = new HighlightDBOpenHelper(context);

    SQLiteDatabase db = highlightDB.getWritableDatabase();

    try {/*from  w  w w.  j  a va 2s .  c  o  m*/
        db.beginTransaction();
        db.delete(HighlightsTable.NAME, HighlightsTable.Columns.EVENT_ID + "=?",
                new String[] { lecture.lecture_id });

        ContentValues values = new ContentValues();

        values.put(HighlightsTable.Columns.EVENT_ID, Integer.parseInt(lecture.lecture_id));
        int highlightState = lecture.highlight ? HighlightsTable.Values.HIGHLIGHT_STATE_ON
                : HighlightsTable.Values.HIGHLIGHT_STATE_OFF;
        values.put(HighlightsTable.Columns.HIGHLIGHT, highlightState);

        db.insert(HighlightsTable.NAME, null, values);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void addAlarm(Context context, Lecture lecture, int alarmTimesIndex) {
    int[] alarm_times = { 0, 5, 10, 15, 30, 45, 60 };
    long when;/*from   w  ww.j a v  a  2s  . co  m*/
    Time time;
    long startTime;
    long startTimeInSeconds = lecture.dateUTC;

    if (startTimeInSeconds > 0) {
        when = startTimeInSeconds;
        startTime = startTimeInSeconds;
        time = new Time();
    } else {
        time = lecture.getTime();
        startTime = time.normalize(true);
        when = time.normalize(true);
    }
    long alarmTimeDiffInSeconds = alarm_times[alarmTimesIndex] * 60 * 1000;
    when -= alarmTimeDiffInSeconds;

    // DEBUG
    // when = System.currentTimeMillis() + (30 * 1000);

    time.set(when);
    MyApp.LogDebug("addAlarm", "Alarm time: " + time.format("%Y-%m-%dT%H:%M:%S%z") + ", in seconds: " + when);

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(BundleKeys.ALARM_LECTURE_ID, lecture.lecture_id);
    intent.putExtra(BundleKeys.ALARM_DAY, lecture.day);
    intent.putExtra(BundleKeys.ALARM_TITLE, lecture.title);
    intent.putExtra(BundleKeys.ALARM_START_TIME, startTime);
    intent.setAction(AlarmReceiver.ALARM_LECTURE);

    intent.setData(Uri.parse("alarm://" + lecture.lecture_id));

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingintent = PendingIntent.getBroadcast(context, Integer.parseInt(lecture.lecture_id),
            intent, 0);

    // Cancel any existing alarms for this lecture
    alarmManager.cancel(pendingintent);

    // Set new alarm
    alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingintent);

    int alarmTimeInMin = alarm_times[alarmTimesIndex];

    // write to DB

    AlarmsDBOpenHelper alarmDB = new AlarmsDBOpenHelper(context);

    SQLiteDatabase db = alarmDB.getWritableDatabase();

    // delete any previous alarms of this lecture
    try {
        db.beginTransaction();
        db.delete(AlarmsTable.NAME, AlarmsTable.Columns.EVENT_ID + "=?", new String[] { lecture.lecture_id });

        ContentValues values = new ContentValues();

        values.put(AlarmsTable.Columns.EVENT_ID, Integer.parseInt(lecture.lecture_id));
        values.put(AlarmsTable.Columns.EVENT_TITLE, lecture.title);
        values.put(AlarmsTable.Columns.ALARM_TIME_IN_MIN, alarmTimeInMin);
        values.put(AlarmsTable.Columns.TIME, when);
        DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
        values.put(AlarmsTable.Columns.TIME_TEXT, df.format(new Date(when)));
        values.put(AlarmsTable.Columns.DISPLAY_TIME, startTime);
        values.put(AlarmsTable.Columns.DAY, lecture.day);

        db.insert(AlarmsTable.NAME, null, values);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
    } finally {
        db.endTransaction();
        db.close();
    }

    lecture.has_alarm = true;
}

From source file:com.contentful.vault.SqliteHelper.java

static void deleteTables(SQLiteDatabase db) {
    String[] columns = new String[] { "name" };
    String selection = "type = ? AND name != ?";
    String[] args = new String[] { "table", "android_metadata" };
    Cursor cursor = db.query("sqlite_master", columns, selection, args, null, null, null);
    List<String> tables = null;
    try {//from   w  w  w . j a v  a 2s . com
        if (cursor.moveToFirst()) {
            tables = new ArrayList<>();
            do {
                tables.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    if (tables != null) {
        db.beginTransaction();
        try {
            for (String table : tables) {
                db.execSQL("DROP TABLE " + escape(table));
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}

From source file:ru.ming13.gambit.database.DatabaseOperator.java

private void deleteDatabaseContents(SQLiteDatabase database) {
    try {/* ww w  .jav  a 2s .  c  o  m*/
        database.beginTransaction();

        database.execSQL(SqlBuilder.buildDeletionClause(DatabaseSchema.Tables.CARDS));
        database.execSQL(SqlBuilder.buildDeletionClause(DatabaseSchema.Tables.DECKS));

        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}

From source file:com.alchemiasoft.common.content.BookDBOpenHelper.java

@Override
public void onCreate(SQLiteDatabase db) {
    try {//from  w w  w  .  j  a va  2 s  .co m
        db.beginTransaction();

        db.execSQL(BookDB.Book.CREATE_TABLE);

        String input = null;
        try {
            input = ResUtil.assetAsString(mContext, Constants.BOOKS_PATH);
        } catch (Exception e) {
            Log.e(TAG_LOG, "Cannot read the input at assets/" + Constants.BOOKS_PATH);
        }
        // Adding the default entries
        if (!TextUtils.isEmpty(input)) {
            Log.d(TAG_LOG, "Adding into the DB: " + input);
            final JSONArray arr = new JSONArray(input);
            final List<Book> books = Book.allFrom(arr);
            // Adding the all the books as a batch
            for (final Book book : books) {
                db.insert(BookDB.Book.TABLE, null, book.toValues());
            }
        }

        db.setTransactionSuccessful();
        Log.i(TAG_LOG, "Successfully created " + BookDB.NAME);
    } catch (Exception e) {
        Log.e(TAG_LOG, "Error creating " + BookDB.NAME + ": ", e);
    } finally {
        db.endTransaction();
    }
}

From source file:edu.htl3r.schoolplanner.backend.database.AutoSelectDatabase.java

private void deleteAllRowsFromDatabaseTransaction(String table, String loginSetKey) {
    SQLiteDatabase database = this.database.openDatabase(true);
    database.beginTransaction();

    this.database.deleteAllRowsWithLoginSetKey(database, table, loginSetKey);

    database.setTransactionSuccessful();
    database.endTransaction();//from w  w w . jav  a  2  s.  c  o m
    this.database.closeDatabase(database);
}

From source file:com.alchemiasoft.common.content.BookDBOpenHelper.java

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {/*from  w  w w . j ava2  s .c o m*/
        db.beginTransaction();

        db.execSQL(BookDB.Book.DELETE_TABLE);

        onCreate(db);

        db.setTransactionSuccessful();
        Log.i(TAG_LOG, "Successfully upgraded " + BookDB.NAME);
    } catch (Exception e) {
        Log.e(TAG_LOG, "Error creating " + BookDB.NAME + ": ", e);
    } finally {
        db.endTransaction();
    }
}

From source file:com.adguard.android.db.DbHelper.java

@Override
public void onCreate(SQLiteDatabase db) {
    LOG.info("DbHelper.onCreate()");
    try {/*from  ww w  .j  av a2s.  c om*/
        db.beginTransaction();

        createTables(db);
        fillFilters(db);
        fillFiltersLocalization(db);
        enableDefaultFilters(db);

        db.setTransactionSuccessful();
    } finally {
        if (db.inTransaction()) {
            db.endTransaction();
        }
    }
}

From source file:com.hufeiya.SignIn.persistence.TopekaDatabaseHelper.java

private void preFillDatabase(SQLiteDatabase db) {
    try {//from   w w w.j av  a 2s . c  o m
        db.beginTransaction();
        try {
            fillCategoriesAndQuizzes(db);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } catch (IOException | JSONException e) {
        Log.e(TAG, "preFillDatabase", e);
    }
}

From source file:com.contentful.vault.SqliteHelper.java

@Override
public void onCreate(SQLiteDatabase db) {
    db.beginTransaction();
    try {/* w ww . j  av  a2 s  .c om*/
        execCreate(spaceHelper, db);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}