List of usage examples for android.database.sqlite SQLiteDatabase setTransactionSuccessful
public void setTransactionSuccessful()
From source file:com.appsimobile.appsii.module.home.provider.HomeContentProvider.java
@Override public int bulkInsert(Uri uri, @NonNull ContentValues[] values) { SqlArguments args = new SqlArguments(uri); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction();//from w ww .ja v a 2s .c o m try { int numValues = values.length; for (int i = 0; i < numValues; i++) { ContentValues value = values[i]; if (db.insert(args.table, null, value) < 0) return 0; } db.setTransactionSuccessful(); } finally { db.endTransaction(); } sendNotify(uri); return values.length; }
From source file:org.path.episample.android.tasks.InitializationTask.java
private void registerCensusForm() { // create a census table in ODK Survey's database SQLiteDatabase db = null; try {//from w ww. ja v a 2s . c o m // db = DatabaseFactory.getInstanceDatabase(context, appName); db = DatabaseFactory.get().getDatabase(getApplication(), getAppName()); List<Column> columns = CensusColumns.USER_DEFINED_COLUMNS; ArrayList<ColumnDefinition> orderedDefs = ColumnDefinition.buildColumnDefinitions(appName, CensusDatabaseHelper.CENSUS_DATABASES_TABLE, columns); db.beginTransaction(); ODKDatabaseUtils.get().createDBTableWithColumns(db, appName, CensusDatabaseHelper.CENSUS_DATABASES_TABLE, orderedDefs); db.setTransactionSuccessful(); registerCensusFormInstanceName(db); } catch (Exception ex) { } finally { if (db != null) { db.endTransaction(); db.close(); } } }
From source file:de.stadtrallye.rallyesoft.model.map.MapManager.java
private void updateDatabase(List<Node> nodes, List<Edge> edges) { SQLiteDatabase db = getDb(); db.beginTransaction();/*from w ww . ja v a2s . co m*/ try { db.delete(Edges.TABLE, null, null); db.delete(Nodes.TABLE, null, null); SQLiteStatement nodeIn = db.compileStatement("INSERT INTO " + Nodes.TABLE + " (" + DatabaseHelper.strStr(Nodes.COLS) + ") VALUES (?, ?, ?, ?, ?)"); SQLiteStatement edgeIn = db.compileStatement( "INSERT INTO " + Edges.TABLE + " (" + DatabaseHelper.strStr(Edges.COLS) + ") VALUES (?, ?, ?)"); for (Node n : nodes) { nodeIn.bindLong(1, n.nodeID); nodeIn.bindString(2, n.name); nodeIn.bindDouble(3, n.location.latitude); nodeIn.bindDouble(4, n.location.longitude); nodeIn.bindString(5, n.description); nodeIn.executeInsert(); } for (Edge m : edges) { edgeIn.bindLong(1, m.nodeA.nodeID); edgeIn.bindLong(2, m.nodeB.nodeID); edgeIn.bindString(3, m.type.toString()); edgeIn.executeInsert(); } db.setTransactionSuccessful(); } catch (Exception e) { Log.e(THIS, "Map Update on Database failed", e); } finally { db.endTransaction(); } }
From source file:com.cyanogenmod.eleven.provider.LocalizedStore.java
private void rebuildLocaleData(LocaleSet locales) { if (DEBUG) {//from w w w . ja va2 s. co m Log.d(TAG, "Locale has changed, rebuilding sorting data"); } final long start = SystemClock.elapsedRealtime(); final SQLiteDatabase db = mMusicDatabase.getWritableDatabase(); db.beginTransaction(); try { db.execSQL("DELETE FROM " + SongSortColumns.TABLE_NAME); db.execSQL("DELETE FROM " + AlbumSortColumns.TABLE_NAME); db.execSQL("DELETE FROM " + ArtistSortColumns.TABLE_NAME); // prep the localization classes mLocaleSetManager.updateLocaleSet(locales); updateLocalizedStore(db, null); // Update the ICU version used to generate the locale derived data // so we can tell when we need to rebuild with new ICU versions. PropertiesStore.getInstance(mContext).storeProperty(PropertiesStore.DbProperties.ICU_VERSION, ICU.getIcuVersion()); PropertiesStore.getInstance(mContext).storeProperty(PropertiesStore.DbProperties.LOCALE, locales.toString()); db.setTransactionSuccessful(); } finally { db.endTransaction(); } if (DEBUG) { Log.i(TAG, "Locale change completed in " + (SystemClock.elapsedRealtime() - start) + "ms"); } }
From source file:org.liberty.android.fantastischmemopro.DatabaseHelper.java
public static void createEmptyDatabase(String path, String name) throws IOException, SQLException { File dbfile = new File(path + "/" + name); if (dbfile.exists()) { // throw new IOException("DB already exist"); /* Create a backup and overwrite it instead poping up an error */ File backupFile = new File(dbfile.getAbsolutePath().replaceAll(".db$", ".old.db")); if (backupFile.exists()) { backupFile.delete();/*from w ww .ja va 2 s. com*/ } dbfile.renameTo(backupFile); } SQLiteDatabase database = SQLiteDatabase.openDatabase(path + "/" + name, null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY); database.execSQL( "CREATE TABLE dict_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, question TEXT, answer TEXT, note TEXT, category TEXT)"); database.execSQL( "CREATE TABLE learn_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, date_learn, interval, grade INTEGER, easiness REAL, acq_reps INTEGER, ret_reps INTEGER, lapses INTEGER, acq_reps_since_lapse INTEGER, ret_reps_since_lapse INTEGER)"); database.execSQL("CREATE TABLE control_tbl(ctrl_key TEXT, value TEXT)"); database.beginTransaction(); try { database.execSQL("DELETE FROM learn_tbl"); database.execSQL("INSERT INTO learn_tbl(_id) SELECT _id FROM dict_tbl"); database.execSQL( "UPDATE learn_tbl SET date_learn = '2010-01-01', interval = 0, grade = 0, easiness = 2.5, acq_reps = 0, ret_reps = 0, lapses = 0, acq_reps_since_lapse = 0, ret_reps_since_lapse = 0"); database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_locale', 'US')"); database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_locale', 'US')"); database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_align', 'center')"); database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_align', 'center')"); database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_font_size', '24')"); database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_font_size', '24')"); database.setTransactionSuccessful(); } finally { database.endTransaction(); database.close(); } }
From source file:org.nuxeo.android.cache.sql.DeferedUpdateTableWrapper.java
public OperationRequest storeRequest(String key, OperationRequest request, OperationType opType) { SQLiteDatabase db = getWritableDatabase(); String sql = "INSERT INTO " + getTableName() + " (" + KEY_COLUMN + "," + OPID_COLUMN + "," + OPTYPE_COLUMN + "," + PARAMS_COLUMN + "," + HEADERS_COLUMN + "," + CTX_COLUMN + "," + DEPS_COLUMN; String operationId = request.getDocumentation().getId(); String jsonParams = new JSONObject(request.getParameters()).toString(); String jsonHeaders = new JSONObject(request.getHeaders()).toString(); String jsonCtx = new JSONObject(request.getContextParameters()).toString(); String deps = request.getDependencies().asJSON(); String sqlValues = " VALUES (" + "'" + key + "'," + "'" + operationId + "'," + "'" + opType.toString() + "'," + "'" + jsonParams + "'," + "'" + jsonHeaders + "'," + "'" + jsonCtx + "'," + "'" + deps + "'"; if (request.getInput() != null) { String inputType = request.getInput().getInputType(); String inputRef = request.getInput().getInputRef(); String inputBin = new Boolean(request.getInput().isBinary()).toString(); sql = sql + "," + INPUT_TYPE_COLUMN + "," + INPUT_REF_COLUMN + "," + INPUT_BINARY_COLUMN; sqlValues = sqlValues + ",'" + inputType + "','" + inputRef + "','" + inputBin + "'"; }// www . j ava2 s. c om String insertQuery = sql + " ) " + sqlValues + ");"; db.beginTransaction(); db.execSQL(insertQuery); db.setTransactionSuccessful(); db.endTransaction(); return request; }
From source file:com.hichinaschool.flashcards.libanki.Collection.java
public synchronized void close(boolean save) { if (mDb != null) { if (!mConf.optBoolean("newBury", false)) { boolean mod = mDb.getMod(); mSched.unburyCards();//w w w . j a v a 2 s . co m mDb.setMod(mod); } try { SQLiteDatabase db = getDb().getDatabase(); if (save) { db.beginTransaction(); try { save(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } else { if (db.inTransaction()) { db.endTransaction(); } lock(); } } catch (RuntimeException e) { AnkiDroidApp.saveExceptionReportFile(e, "closeDB"); } AnkiDatabaseManager.closeDatabase(mPath); mDb = null; mMedia.close(); // Log.i(AnkiDroidApp.TAG, "Collection closed"); } }
From source file:com.google.android.apps.santatracker.service.APIProcessor.java
private int processStream(JSONArray json, boolean isWear) { SQLiteDatabase db = mStreamDBHelper.getWritableDatabase(); db.beginTransaction();/* w w w . j a v a2 s . co m*/ try { // loop over each card int i; for (i = 0; i < json.length(); i++) { JSONObject card = json.getJSONObject(i); final long timestamp = card.getLong(FIELD_STREAM_TIMESTAMP); final String status = getExistingJSONString(card, FIELD_STREAM_STATUS); final String didYouKnow = getExistingJSONString(card, FIELD_STREAM_DIDYOUKNOW); final String imageUrl = getExistingJSONString(card, FIELD_STREAM_IMAGEURL); final String youtubeId = getExistingJSONString(card, FIELD_STREAM_YOUTUBEID); // if (mDebugLog) { // Log.d(TAG, "Notification: " + timestamp); // } try { // All parsed, insert into DB mStreamDBHelper.insert(db, timestamp, status, didYouKnow, imageUrl, youtubeId, isWear); } catch (android.database.sqlite.SQLiteConstraintException e) { // ignore duplicate cards } } db.setTransactionSuccessful(); return i; } catch (JSONException e) { Log.d(TAG, "Santa location tracking error 31"); SantaLog.d(TAG, "JSON Exception", e); } finally { db.endTransaction(); } return 0; }
From source file:com.ichi2.libanki.Collection.java
public synchronized void close(boolean save) { if (mDb != null) { if (!mConf.optBoolean("newBury", false)) { boolean mod = mDb.getMod(); mSched.unburyCards();//from w w w . j a va2 s. c o m mDb.setMod(mod); } try { SQLiteDatabase db = getDb().getDatabase(); if (save) { db.beginTransaction(); try { save(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } else { if (db.inTransaction()) { db.endTransaction(); } lock(); } } catch (RuntimeException e) { AnkiDroidApp.sendExceptionReport(e, "closeDB"); } AnkiDatabaseManager.closeDatabase(mPath); mDb = null; mMedia.close(); _closeLog(); Timber.i("Collection closed"); } }
From source file:org.ohmage.db.DbHelper.java
/** * Used by the ContentProvider to insert a campaign and also insert into * interested tables. Don't use this directly; if you do, none of the * contentobservers, etc. that are listening to Campaigns, Surveys, or * SurveyPrompts will be notified./*from ww w. j ava2s. c o m*/ * * @param values * a ContentValues collection, preferably generated by calling * {@link Campaign}'s toCV() method * @return the ID of the inserted record */ public long addCampaign(SQLiteDatabase db, ContentValues values) { long rowId = -1; // the row ID for the campaign that we'll eventually be // returning try { // start the transaction that will include inserting the campaign + // surveys + survey prompts db.beginTransaction(); // hold onto some variables for processing String configurationXml = values.getAsString(Campaigns.CAMPAIGN_CONFIGURATION_XML); String campaignUrn = values.getAsString(Campaigns.CAMPAIGN_URN); // actually insert the campaign rowId = db.insert(Tables.CAMPAIGNS, null, values); if (configurationXml != null) { // xml parsing below, inserts into Surveys and SurveyPrompts if (populateSurveysFromCampaignXML(db, campaignUrn, configurationXml)) { // i think we're done now; finish up the transaction db.setTransactionSuccessful(); } // else we fail and the transaction gets rolled back } else { db.setTransactionSuccessful(); } } finally { db.endTransaction(); } return rowId; }