当前位置: 首页>>代码示例>>Java>>正文


Java SQLiteDatabase.beginTransaction方法代码示例

本文整理汇总了Java中android.database.sqlite.SQLiteDatabase.beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java SQLiteDatabase.beginTransaction方法的具体用法?Java SQLiteDatabase.beginTransaction怎么用?Java SQLiteDatabase.beginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.database.sqlite.SQLiteDatabase的用法示例。


在下文中一共展示了SQLiteDatabase.beginTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: removeFromDocument

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
protected <D extends Document<I>, I> void removeFromDocument(long messageId, String column, I object, Class<D> clazz) throws IOException {
  SQLiteDatabase database = databaseHelper.getWritableDatabase();
  database.beginTransaction();

  try {
    D           document = getDocument(database, messageId, column, clazz);
    Iterator<I> iterator = document.getList().iterator();

    while (iterator.hasNext()) {
      I item = iterator.next();

      if (item.equals(object)) {
        iterator.remove();
        break;
      }
    }

    setDocument(database, messageId, column, document);
    database.setTransactionSuccessful();
  } finally {
    database.endTransaction();
  }
}
 
开发者ID:XecureIT,项目名称:PeSanKita-android,代码行数:24,代码来源:MessagingDatabase.java

示例2: isFavorite

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
public boolean isFavorite(long songId) {
    final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
    database.beginTransaction();

    Cursor cursor = null;
    try {
        cursor = database.query(FavoriteSongColumns.NAME, new String[]{FavoriteSongColumns.SONGID},
                FavoriteSongColumns.SONGID + " =? ", new String[]{String.valueOf(songId)}, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            return true;
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    }
    return false;
}
 
开发者ID:komamj,项目名称:KomaMusic,代码行数:21,代码来源:FavoriteSong.java

示例3: removeFavoriteSong

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
public int removeFavoriteSong(final long[] songId) {
    final SQLiteDatabase database = mMusicDatabase.getWritableDatabase();
    database.beginTransaction();

    Cursor cursor = null;
    int deleted = 0;
    try {
        for (long aSongId : songId) {
            cursor = database.query(FavoriteSongColumns.NAME, new String[]{FavoriteSongColumns.SONGID},
                    FavoriteSongColumns.SONGID + " =? ", new String[]{String.valueOf(aSongId)}, null, null, null);
            if (cursor != null && cursor.getCount() >= 0) {
                database.delete(FavoriteSongColumns.NAME, FavoriteSongColumns.SONGID + " =? ",
                        new String[]{String.valueOf(aSongId)});
                deleted++;
            }
        }
        return deleted;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    }
}
 
开发者ID:komamj,项目名称:KomaMusic,代码行数:26,代码来源:FavoriteSong.java

示例4: runCase

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
@Override
public Metrics runCase() {
    mDbHelper = new DbHelper(App.getInstance(), IntegerInsertsTransactionCase.class.getSimpleName());
    Metrics result = new Metrics(getClass().getSimpleName()+" ("+mInsertions+" insertions)", mTestSizeIndex);
    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    result.started();
    db.beginTransaction();
    ContentValues values = new ContentValues(1);
    for (int i = 0; i < mInsertions; i++) {
        values.put("val", mRandom.nextInt());
        db.insert("inserts_1", null, values);
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    result.finished();
    return result;
}
 
开发者ID:jasonwyatt,项目名称:SQLite-Performance,代码行数:18,代码来源:IntegerInsertsTransactionCase.java

示例5: GetQid

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
public List<Integer> GetQid() {
    String coloumns[] = {CORRECTNO};
    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();
    Cursor cursor = db.query(TABLE_NAME2, coloumns, null, null, null, null, null);
    List<Integer> list = new ArrayList<>();

    while (cursor.moveToNext()) {
        int questionId = cursor.getInt(0);
        list.add(questionId);
    }

    db.setTransactionSuccessful();
    db.endTransaction();
    cursor.close();
    db.close();
    return list;
}
 
开发者ID:sarveshchavan7,项目名称:Trivia-Knowledge,代码行数:20,代码来源:DemoHelperClass.java

示例6: transactionAround

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
@Override
public void transactionAround(boolean readOnly, CallBack callback)
{
	SQLiteDatabase sqLiteDatabase = null;
	if (readOnly)
	{
		sqLiteDatabase = this.getReadableDatabase();
	}
	else
	{
		sqLiteDatabase = this.getWritableDatabase();
	}
	sqLiteDatabase.beginTransaction();
	callback.doSomething(sqLiteDatabase);
	sqLiteDatabase.setTransactionSuccessful();
	sqLiteDatabase.endTransaction();
}
 
开发者ID:stytooldex,项目名称:stynico,代码行数:18,代码来源:SQLManager.java

示例7: upsert

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
@SuppressWarnings("SameParameterValue")
private void upsert(@NonNull SQLiteDatabase db, @NonNull String table, @NonNull ContentValues values, @NonNull String idCol) {
	try {
		db.beginTransaction();
		String   where = idCol + " = ?";
		String[] args  = {values.getAsString(idCol)};
		int      rows  = db.update(table, values, where, args);
		if (rows == 0) {
			long inserted = db.insert(table, null, values);
			if (inserted == -1) {
				throw new SQLException("failed to insert row into '" + table + "' - see logcat.");
			}
		}
		db.setTransactionSuccessful();
	} finally {
		db.endTransaction();
	}
}
 
开发者ID:lambdasoup,项目名称:blockvote,代码行数:19,代码来源:StatsProvider.java

示例8: runCase

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
@Override
public Metrics runCase() {
    mDbHelper = new DbHelper(App.getInstance(), getClass().getName());
    Metrics result = new Metrics(getClass().getSimpleName()+" ("+mInsertions+" insertions)", mTestSizeIndex);
    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    byte[] titleByteArry = new byte[50];
    byte[] urlByteArray = new byte[100];
    byte[] lyricsByteArray = new byte[2000];
    byte[] aboutByteArray = new byte[2000];
    Map<Integer, SQLiteStatement> statementCache = new HashMap<>();

    result.started();
    db.beginTransaction();
    mInsertId = 1;
    doInsertions(db, mInsertions, statementCache, titleByteArry, urlByteArray, lyricsByteArray, aboutByteArray);
    db.setTransactionSuccessful();
    db.endTransaction();
    result.finished();

    return result;
}
 
开发者ID:jasonwyatt,项目名称:SQLite-Performance,代码行数:23,代码来源:BatchedSQLiteStatementTestCase.java

示例9: applyBatch

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    if (DBG) Log.d(TAG, "applyBatch");
    ContentProviderResult[] result = null;
    SQLiteDatabase db = mDbHolder.get();
    db.beginTransaction();
    try {
         result = super.applyBatch(operations);
         db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    if (result != null) {
        mCr.notifyChange(MusicStore.ALL_CONTENT_URI, null);
    }
    return result;
}
 
开发者ID:archos-sa,项目名称:aos-MediaLib,代码行数:19,代码来源:MusicProvider.java

示例10: bulkInsert

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
/**
 * Handles requests to insert a set of new rows. In Sunshine, we are only going to be
 * inserting multiple rows of data at a time from a weather forecast. There is no use case
 * for inserting a single row of data into our ContentProvider, and so we are only going to
 * implement bulkInsert. In a normal ContentProvider's implementation, you will probably want
 * to provide proper functionality for the insert method as well.
 *
 * @param uri    The content:// URI of the insertion request.
 * @param values An array of sets of column_name/value pairs to add to the database.
 *               This must not be {@code null}.
 *
 * @return The number of values that were inserted.
 */
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();

    switch (sUriMatcher.match(uri)) {

        case CODE_WEATHER:
            db.beginTransaction();
            int rowsInserted = 0;
            try {
                for (ContentValues value : values) {
                    long weatherDate =
                            value.getAsLong(WeatherContract.WeatherEntry.COLUMN_DATE);
                    if (!SunshineDateUtils.isDateNormalized(weatherDate)) {
                        throw new IllegalArgumentException("Date must be normalized to insert");
                    }

                    long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value);
                    if (_id != -1) {
                        rowsInserted++;
                    }
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }

            if (rowsInserted > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }

            return rowsInserted;

        default:
            return super.bulkInsert(uri, values);
    }
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:51,代码来源:WeatherProvider.java

示例11: getAllOfTheQuestions

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
List<TriviaQuestion> getAllOfTheQuestions() {

        List<TriviaQuestion> questionsList = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        String coloumn[] = {UID, QUESTION, OPTA, OPTB, OPTC, OPTD, ANSWER};
        Cursor cursor = db.query(TABLE_NAME, coloumn, null, null, null, null, null);


        while (cursor.moveToNext()) {
            TriviaQuestion question = new TriviaQuestion();
            question.setId(cursor.getInt(0));
            question.setQuestion(cursor.getString(1));
            question.setOptA(cursor.getString(2));
            question.setOptB(cursor.getString(3));
            question.setOptC(cursor.getString(4));
            question.setOptD(cursor.getString(5));
            question.setAnswer(cursor.getString(6));
            questionsList.add(question);
        }

        db.setTransactionSuccessful();
        db.endTransaction();
        cursor.close();
        db.close();
        return questionsList;
    }
 
开发者ID:sarveshchavan7,项目名称:Quiz-Game,代码行数:28,代码来源:TriviaQuizHelper.java

示例12: replaceRate

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
public void replaceRate(ExchangeRate rate, long originalDate) {
    SQLiteDatabase db = db();
    db.beginTransaction();
    try {
        replaceRateInTransaction(rate, originalDate, db);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
 
开发者ID:tiberiusteng,项目名称:financisto1-holo,代码行数:11,代码来源:DatabaseAdapter.java

示例13: updateCache

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
private void updateCache(CustomPropertyType[] rgcpt) {
    // note that these calls will close the db, so we do them first.
    DBCache dbc = new DBCache();
    dbc.flushCache(TABLENAME, true);
    Boolean fResult = false;

    // now, we get an open db
    SQLiteDatabase db = MFBMain.mDBHelper.getWritableDatabase();

    try {
        // I've read that multiple inserts are much faster inside a
        // transaction.
        db.beginTransaction();
        try {
            for (CustomPropertyType cpt : rgcpt) {
                ContentValues cv = new ContentValues();
                cpt.ToContentValues(cv);

                long l = db.insertOrThrow(TABLENAME, null, cv);
                if (l < 0)
                    throw new Error("Error inserting CustomPropertyType");
            }
            db.setTransactionSuccessful();
            fResult = true;
        } catch (Exception ex) {
            this.setLastError(ex.getMessage());
        } finally {
            db.endTransaction();
        }
    } catch (Exception e) {
        this.setLastError(e.getMessage());
    }

    if (fResult)
        dbc.updateCache(TABLENAME);
}
 
开发者ID:ericberman,项目名称:MyFlightbookAndroid,代码行数:37,代码来源:CustomPropertyTypesSvc.java

示例14: deleteEmptyFolders

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
/**
 * Deletes any empty folder from the DB.
 * @return Ids of deleted folders.
 */
private ArrayList<Long> deleteEmptyFolders() {
    ArrayList<Long> folderIds = new ArrayList<>();
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    db.beginTransaction();
    try {
        // Select folders whose id do not match any container value.
        String selection = LauncherSettings.Favorites.ITEM_TYPE + " = "
                + LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND "
                + LauncherSettings.Favorites._ID +  " NOT IN (SELECT " +
                        LauncherSettings.Favorites.CONTAINER + " FROM "
                            + Favorites.TABLE_NAME + ")";
        Cursor c = db.query(Favorites.TABLE_NAME,
                new String[] {LauncherSettings.Favorites._ID},
                selection, null, null, null, null);
        while (c.moveToNext()) {
            folderIds.add(c.getLong(0));
        }
        c.close();
        if (!folderIds.isEmpty()) {
            db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery(
                    LauncherSettings.Favorites._ID, folderIds), null);
        }
        db.setTransactionSuccessful();
    } catch (SQLException ex) {
        Log.e(TAG, ex.getMessage(), ex);
        folderIds.clear();
    } finally {
        db.endTransaction();
    }
    return folderIds;
}
 
开发者ID:TeamBrainStorm,项目名称:SimpleUILauncher,代码行数:36,代码来源:LauncherProvider.java

示例15: saveDaily

import android.database.sqlite.SQLiteDatabase; //导入方法依赖的package包/类
public void saveDaily(List<DailyPvDatum> dailyPvData) {
    SQLiteDatabase db = pvDataHelper.getWritableDatabase();

    db.beginTransaction();
    String sql = "REPLACE INTO " + PvDataContract.DailyPvData.TABLE_NAME +
            "(" + PvDataContract.DailyPvData.COLUMN_NAME_YEAR +
            "," + PvDataContract.DailyPvData.COLUMN_NAME_MONTH +
            "," + PvDataContract.DailyPvData.COLUMN_NAME_DAY +
            "," + PvDataContract.DailyPvData.COLUMN_NAME_ENERGY_GENERATED +
            "," + PvDataContract.DailyPvData.COLUMN_NAME_PEAK_POWER +
            "," + PvDataContract.DailyPvData.COLUMN_NAME_CONDITION +
            ") VALUES (?,?,?,?,?,?);";
    SQLiteStatement statement = db.compileStatement(sql);
    double maxEnergyGenerated = Double.MIN_VALUE;
    for (DailyPvDatum dailyPvDatum : dailyPvData) {
        statement.clearBindings();
        statement.bindLong(1, dailyPvDatum.getYear());
        statement.bindLong(2, dailyPvDatum.getMonth());
        statement.bindLong(3, dailyPvDatum.getDay());
        statement.bindDouble(4, dailyPvDatum.getEnergyGenerated());
        statement.bindDouble(5, dailyPvDatum.getPeakPower());
        statement.bindString(6, dailyPvDatum.getCondition());
        statement.execute();
        maxEnergyGenerated = Math.max(maxEnergyGenerated, dailyPvDatum.getEnergyGenerated());
    }
    db.setTransactionSuccessful();
    db.endTransaction();

    db.close();
    Log.d(TAG, "Saved " + dailyPvData.size() + " rows of daily PV data to database");

    RecordPvDatum recordPvDatum = loadRecord();
    if (maxEnergyGenerated > recordPvDatum.getDailyEnergyGenerated()) {
        recordPvDatum.setDailyEnergyGenerated(maxEnergyGenerated);
        saveRecord(recordPvDatum);
    }
}
 
开发者ID:jansipke,项目名称:pvdisplay,代码行数:38,代码来源:PvDataOperations.java


注:本文中的android.database.sqlite.SQLiteDatabase.beginTransaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。