类android.database.sqlite.SQLiteOpenHelper源码实例Demo

下面列出了怎么用android.database.sqlite.SQLiteOpenHelper的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: reacteu-app   文件: HistoryManager.java
public void trimHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();      
    cursor = db.query(DBHelper.TABLE_NAME,
                      ID_COL_PROJECTION,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(MAX_ITEMS);
    while (cursor.moveToNext()) {
      db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
    }
  } catch (SQLiteException sqle) {
    // We're seeing an error here when called in CaptureActivity.onCreate() in rare cases
    // and don't understand it. First theory is that it's transient so can be safely ignored.
    // TODO revisit this after live in a future version to see if it 'worked'
    Log.w(TAG, sqle);
    // continue
  } finally {
    close(cursor, db);
  }
}
 
public void deleteHistoryItem(int number) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();      
    cursor = db.query(DBHelper.TABLE_NAME,
                      ID_COL_PROJECTION,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(number + 1);
    db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
  } finally {
    close(cursor, db);
  }
}
 
public void trimHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();      
    cursor = db.query(DBHelper.TABLE_NAME,
                      ID_COL_PROJECTION,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(MAX_ITEMS);
    while (cursor.moveToNext()) {
      String id = cursor.getString(0);
      Log.i(TAG, "Deleting scan history ID " + id);
      db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
    }
  } catch (SQLiteException sqle) {
    // We're seeing an error here when called in CaptureActivity.onCreate() in rare cases
    // and don't understand it. First theory is that it's transient so can be safely ignored.
    Log.w(TAG, sqle);
    // continue
  } finally {
    close(cursor, db);
  }
}
 
源代码4 项目: reacteu-app   文件: HistoryManager.java
public void deleteHistoryItem(int number) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();      
    cursor = db.query(DBHelper.TABLE_NAME,
                      ID_COL_PROJECTION,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(number + 1);
    db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
  } finally {
    close(cursor, db);
  }
}
 
源代码5 项目: weex   文件: HistoryManager.java
public HistoryItem buildHistoryItem(int number) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getReadableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(number + 1);
    String text = cursor.getString(0);
    String display = cursor.getString(1);
    String format = cursor.getString(2);
    long timestamp = cursor.getLong(3);
    String details = cursor.getString(4);
    Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
    return new HistoryItem(result, display, details);
  } finally {
    close(cursor, db);
  }
}
 
public int getDownloadCountForTopic(SQLiteOpenHelper dbh, String topicId, int depth) {
	Log.d(LOG_TAG, "getDownloadCountForTopic");
	
	int result = 0;
	SQLiteDatabase db = dbh.getReadableDatabase();
	
	for (int i = 0; i < depth; ++i) {
		String sql = buildDownloadCountQuery(i);
		Cursor c = db.rawQuery(sql, new String[] {topicId});
		c.moveToFirst();
		result += c.getInt(0);
		Log.d(LOG_TAG, " result is " + result);
		c.close();
	}
	
	return result;
}
 
源代码7 项目: barcodescanner-lib-aar   文件: HistoryManager.java
public HistoryItem buildHistoryItem(int number) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getReadableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(number + 1);
    String text = cursor.getString(0);
    String display = cursor.getString(1);
    String format = cursor.getString(2);
    long timestamp = cursor.getLong(3);
    String details = cursor.getString(4);
    Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
    return new HistoryItem(result, display, details);
  } finally {
    close(cursor, db);
  }
}
 
public HistoryItem buildHistoryItem(int number) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getReadableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null, DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(number + 1);
    String text = cursor.getString(0);
    String display = cursor.getString(1);
    String format = cursor.getString(2);
    long timestamp = cursor.getLong(3);
    String details = cursor.getString(4);
    Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
    return new HistoryItem(result, display, details);
  } finally {
    close(cursor, db);
  }
}
 
public boolean hasHistoryItems() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getReadableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
    cursor.moveToFirst();
    return cursor.getInt(0) > 0;
  } finally {
    close(cursor, db);
  }
}
 
源代码10 项目: itracing2   文件: SQLiteCursorLoader.java
@Override
protected Void doInBackground(Object... params)
{
    SQLiteOpenHelper db = (SQLiteOpenHelper) params[0];
    String table = (String) params[1];
    String nullColumnHack = (String) params[2];
    ContentValues values = (ContentValues) params[3];

    db.getWritableDatabase().replace(table, nullColumnHack, values);

    return (null);
}
 
源代码11 项目: storio   文件: DefaultStorIOSQLiteTest.java
@SuppressWarnings("unchecked")
@Test
public void instantiatePutContentValuesVarArgs() {
    DefaultStorIOSQLite.builder()
            .sqliteOpenHelper(mock(SQLiteOpenHelper.class))
            .build()
            .put()
            .contentValues(mock(ContentValues.class), mock(ContentValues.class))
            .withPutResolver(mock(PutResolver.class))
            .prepare();
}
 
源代码12 项目: incubator-weex-playground   文件: HistoryManager.java
/**
 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
 * line, terminated by a line break (\r\n). The values in each line are comma-separated,
 * and double-quoted. Double-quotes within values are escaped with a sequence of two
 * double-quotes. The fields output are:</p>
 *
 * <ol>
 *  <li>Raw text</li>
 *  <li>Display text</li>
 *  <li>Format (e.g. QR_CODE)</li>
 *  <li>Unix timestamp (milliseconds since the epoch)</li>
 *  <li>Formatted version of timestamp</li>
 *  <li>Supplemental info (e.g. price info for a product barcode)</li>
 * </ol>
 */
CharSequence buildHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME,
                      COLUMNS,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");

    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    StringBuilder historyText = new StringBuilder(1000);
    while (cursor.moveToNext()) {

      historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");

      // Add timestamp again, formatted
      long timestamp = cursor.getLong(3);
      historyText.append('"').append(massageHistoryField(
          format.format(new Date(timestamp)))).append("\",");

      // Above we're preserving the old ordering of columns which had formatted data in position 5

      historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
    }
    return historyText;
  } finally {
    close(cursor, db);
  }
}
 
源代码13 项目: itracing2   文件: SQLiteCursorLoader.java
@Override
protected Void doInBackground(Object... params)
{
    SQLiteOpenHelper db = (SQLiteOpenHelper) params[0];
    String table = (String) params[1];
    String where = (String) params[2];
    String[] whereParams = (String[]) params[3];

    db.getWritableDatabase().delete(table, where, whereParams);

    return (null);
}
 
源代码14 项目: storio   文件: DefaultStorIOSQLite.java
protected DefaultStorIOSQLite(
        @NonNull SQLiteOpenHelper sqLiteOpenHelper,
        @NonNull TypeMappingFinder typeMappingFinder,
        @Nullable Scheduler defaultRxScheduler,
        @NonNull List<Interceptor> interceptors) {
    this.sqLiteOpenHelper = sqLiteOpenHelper;
    this.defaultRxScheduler = defaultRxScheduler;
    this.interceptors = unmodifiableNonNullList(interceptors);
    lowLevel = new LowLevelImpl(typeMappingFinder);
}
 
源代码15 项目: reacteu-app   文件: HistoryManager.java
public void addHistoryItem(Result result, ResultHandler handler) {
  // Do not save this item to the history if the preference is turned off, or the contents are
  // considered secure.
  if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true) ||
      handler.areContentsSecure()) {
    return;
  }

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
  if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
    deletePrevious(result.getText());
  }

  ContentValues values = new ContentValues();
  values.put(DBHelper.TEXT_COL, result.getText());
  values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
  values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
  values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());

  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    // Insert the new entry into the DB.
    db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
  } finally {
    close(null, db);
  }
}
 
源代码16 项目: MiBandDecompiled   文件: i.java
public Cursor a(String s)
{
    SQLiteOpenHelper sqliteopenhelper = g;
    Cursor cursor = null;
    if (sqliteopenhelper == null)
    {
        break MISSING_BLOCK_LABEL_68;
    }
    String s1 = "select * from analytics ";
    if (s != null)
    {
        s1 = String.format("%s where %s", new Object[] {
            "select * from analytics ", s
        });
    }
    SQLiteDatabase sqlitedatabase;
    Cursor cursor1;
    try
    {
        sqlitedatabase = g.getReadableDatabase();
    }
    catch (SQLiteException sqliteexception)
    {
        Object aobj[] = new Object[1];
        aobj[0] = g.getDatabaseName();
        Log.e("ANALYTICS.SQLITESTORE", String.format("can't read database:%s", aobj));
        return null;
    }
    cursor = null;
    if (sqlitedatabase == null)
    {
        break MISSING_BLOCK_LABEL_68;
    }
    cursor1 = sqlitedatabase.rawQuery(s1, null);
    cursor = cursor1;
    return cursor;
}
 
源代码17 项目: android-skeleton-project   文件: DataSQLiteDB.java
@Override
public SQLiteOpenHelper getHelper() {
    if (helper == null) {
        helper = new DatabaseHelper(context, dbName, dbVersion, this);
        helper.getWritableDatabase(); //results in db onCreate being called
    }
    return helper;
}
 
源代码18 项目: itracing2   文件: SQLiteCursorLoader.java
/**
 * Creates a fully-specified SQLiteCursorLoader. See
 * {@link SQLiteDatabase#rawQuery(String, String[], CancellationSignal)}
 * SQLiteDatabase.rawQuery()} for documentation on the
 * meaning of the parameters. These will be passed as-is
 * to that call.
 */
public SQLiteCursorLoader(Context context, SQLiteOpenHelper db,
                          String rawQuery, String[] args)
{
    super(context);
    this.db = db;
    this.rawQuery = rawQuery;
    this.args = args;
}
 
源代码19 项目: storio   文件: DefaultStorIOSQLiteTest.java
@SuppressWarnings("unchecked")
@Test
public void instantiatePutContentValues() {
    DefaultStorIOSQLite.builder()
            .sqliteOpenHelper(mock(SQLiteOpenHelper.class))
            .build()
            .put()
            .contentValues(mock(ContentValues.class))
            .withPutResolver(mock(PutResolver.class))
            .prepare();
}
 
源代码20 项目: Study_Android_Demo   文件: HistoryManager.java
/**
 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
 * line, terminated by a line break (\r\n). The values in each line are comma-separated,
 * and double-quoted. Double-quotes within values are escaped with a sequence of two
 * double-quotes. The fields output are:</p>
 *
 * <ol>
 *  <li>Raw text</li>
 *  <li>Display text</li>
 *  <li>Format (e.g. QR_CODE)</li>
 *  <li>Unix timestamp (milliseconds since the epoch)</li>
 *  <li>Formatted version of timestamp</li>
 *  <li>Supplemental info (e.g. price info for a product barcode)</li>
 * </ol>
 */
CharSequence buildHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME,
                      COLUMNS,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");

    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    StringBuilder historyText = new StringBuilder(1000);
    while (cursor.moveToNext()) {

      historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");

      // Add timestamp again, formatted
      long timestamp = cursor.getLong(3);
      historyText.append('"').append(massageHistoryField(
          format.format(new Date(timestamp)))).append("\",");

      // Above we're preserving the old ordering of columns which had formatted data in position 5

      historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
    }
    return historyText;
  } finally {
    close(cursor, db);
  }
}
 
源代码21 项目: zxingfragmentlib   文件: HistoryManager.java
void clearHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, null, null);
  } finally {
    close(null, db);
  }
}
 
源代码22 项目: Study_Android_Demo   文件: HistoryManager.java
void clearHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, null, null);
  } finally {
    close(null, db);
  }
}
 
源代码23 项目: storio   文件: DesignTestStorIOSQLite.java
@NonNull
@Override
public SQLiteOpenHelper sqliteOpenHelper() {
    // not required in design test
    // noinspection ConstantConditions
    return null;
}
 
源代码24 项目: ZXing-Standalone-library   文件: HistoryManager.java
public void addHistoryItem(Result result, ResultHandler handler) {
  // Do not save this item to the history if the preference is turned off, or the contents are
  // considered secure.
  if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true) ||
      handler.areContentsSecure() || !enableHistory) {
    return;
  }

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
  if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
    deletePrevious(result.getText());
  }

  ContentValues values = new ContentValues();
  values.put(DBHelper.TEXT_COL, result.getText());
  values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
  values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
  values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());

  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    // Insert the new entry into the DB.
    db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
  } finally {
    close(null, db);
  }
}
 
源代码25 项目: android-apps   文件: HistoryManager.java
void clearHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, null, null);
  } finally {
    close(null, db);
  }
}
 
源代码26 项目: weex   文件: HistoryManager.java
void clearHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, null, null);
  } finally {
    close(null, db);
  }
}
 
源代码27 项目: ZXing-Standalone-library   文件: HistoryManager.java
/**
 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
 * line, terminated by a line break (\r\n). The values in each line are comma-separated,
 * and double-quoted. Double-quotes within values are escaped with a sequence of two
 * double-quotes. The fields output are:</p>
 *
 * <ol>
 *  <li>Raw text</li>
 *  <li>Display text</li>
 *  <li>Format (e.g. QR_CODE)</li>
 *  <li>Unix timestamp (milliseconds since the epoch)</li>
 *  <li>Formatted version of timestamp</li>
 *  <li>Supplemental info (e.g. price info for a product barcode)</li>
 * </ol>
 */
CharSequence buildHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME,
                      COLUMNS,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");

    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    StringBuilder historyText = new StringBuilder(1000);
    while (cursor.moveToNext()) {

      historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");

      // Add timestamp again, formatted
      long timestamp = cursor.getLong(3);
      historyText.append('"').append(massageHistoryField(
          format.format(new Date(timestamp)))).append("\",");

      // Above we're preserving the old ordering of columns which had formatted data in position 5

      historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
    }
    return historyText;
  } finally {
    close(cursor, db);
  }
}
 
源代码28 项目: AnDevCon-RxPatterns   文件: Example2.java
public StorIOSQLite provideStorIOSQLite(@NonNull SQLiteOpenHelper sqLiteOpenHelper) {
    return DefaultStorIOSQLite.builder()
            .sqliteOpenHelper(sqLiteOpenHelper)
            // ItemSQLiteTypeMapping is auto-generated at compile-time by StorIO
            .addTypeMapping(Item.class, new ItemSQLiteTypeMapping())
            .build();
}
 
源代码29 项目: itracing2   文件: SQLiteCursorLoader.java
@Override
protected Void doInBackground(Object... params)
{
    SQLiteOpenHelper db = (SQLiteOpenHelper) params[0];
    String table = (String) params[1];
    ContentValues values = (ContentValues) params[2];
    String where = (String) params[3];
    String[] whereParams = (String[]) params[4];

    db.getWritableDatabase()
            .update(table, values, where, whereParams);

    return (null);
}
 
源代码30 项目: reacteu-app   文件: HistoryManager.java
private void deletePrevious(String text) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
  } finally {
    close(null, db);
  }
}
 
 类所在包