本文整理汇总了Java中android.database.sqlite.SQLiteOpenHelper类的典型用法代码示例。如果您正苦于以下问题:Java SQLiteOpenHelper类的具体用法?Java SQLiteOpenHelper怎么用?Java SQLiteOpenHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SQLiteOpenHelper类属于android.database.sqlite包,在下文中一共展示了SQLiteOpenHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: borrowObject
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
public SQLiteDatabaseWrapper borrowObject() throws Exception {
synchronized (pool) {
if (!initialized) {
for (int i = 0; i < connections; i++) {
// Initialize!
SQLiteOpenHelper outerSqLiteOpenHelper;
if(dbName == null)
outerSqLiteOpenHelper = createSQLiteOpenHelperInstance();
else
outerSqLiteOpenHelper = createSQLiteOpenHelperInstance(dbName);
pool.add(new SQLiteDatabaseWrapper(outerSqLiteOpenHelper));
}
initialized = true;
}
// wait until there is an available connection
while (pool.size() == 0) {
pool.wait();
}
return pool.remove(0);
}
}
示例2: buildHistoryItems
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
public List<HistoryItem> buildHistoryItems() {
SQLiteOpenHelper helper = new DBHelper(activity);
List<HistoryItem> items = new ArrayList<>();
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");
while (cursor.moveToNext()) {
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);
items.add(new HistoryItem(result, display, details));
}
} finally {
close(cursor, db);
}
return items;
}
示例3: buildHistoryItem
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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);
}
}
示例4: deleteHistoryItem
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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: trimHistory
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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);
}
}
示例6: sequentialUpgrade
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
/**
* Upgrade existing database.
*/
@Test
public void sequentialUpgrade() {
final Context ctx = RuntimeEnvironment.application.getApplicationContext();
final int[] versions = new int[]{1, 2, 3};
for (int i = 1; i <= 3; i++) {
final SQLiteOpenHelper unit = new DBUnit(ctx, i);
final SQLiteDatabase db = unit.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT value FROM versions", new String[0]);
assertVersions(cursor, Arrays.copyOf(versions, i));
} finally {
if (cursor != null) {
cursor.close();
}
db.close();
}
unit.close();
}
}
示例7: customFolder
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
/**
* Apply migrations from custom folder.
*/
@Test
public void customFolder() {
final Context ctx = RuntimeEnvironment.application;
final SQLiteOpenHelper unit = new DBUnit(ctx, 1, "custom_migrations");
final SQLiteDatabase database = unit.getReadableDatabase();
Cursor cursor = null;
try {
cursor = database.rawQuery("SELECT value FROM custom_versions", new String[0]);
assertVersions(cursor, 1);
} finally {
if (cursor != null) {
cursor.close();
}
database.close();
}
}
示例8: provideDatabase
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
@Provides
@Singleton
BriteDatabase provideDatabase(SqlBrite sqlBrite, SQLiteOpenHelper helper) {
BriteDatabase db = sqlBrite.wrapDatabaseHelper(helper, Schedulers.io());
db.setLoggingEnabled(true);
return db;
}
示例9: createSQLiteOpenHelperInstance
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
protected SQLiteOpenHelper createSQLiteOpenHelperInstance() throws Exception {
// We want the constructor with Context parameter
Constructor<?> cons = sqliteOpenHelperClass.getConstructor(new Class[] { Context.class });
// We create the instance!
return (SQLiteOpenHelper) cons.newInstance(context);
}
示例10: onCreate
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
@Override
public boolean onCreate()
{
try
{
SQLiteOpenHelper databaseHelper = new Database(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
database = databaseHelper.getWritableDatabase();
return (database != null) ? true : false;
}
catch (Exception e)
{
Log.e("DatabaseProvider", "onCreate : " + e);
return false;
}
}
示例11: hasHistoryItems
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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);
}
}
示例12: addHistoryItem
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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);
}
}
示例13: deletePrevious
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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);
}
}
示例14: buildHistory
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
/**
* <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);
}
}
示例15: clearHistory
import android.database.sqlite.SQLiteOpenHelper; //导入依赖的package包/类
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);
}
}