Java Code Examples for android.database.sqlite.SQLiteOpenHelper#getWritableDatabase()
The following examples show how to use
android.database.sqlite.SQLiteOpenHelper#getWritableDatabase() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HistoryManager.java From reacteu-app with MIT License | 7 votes |
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); } }
Example 2
Source File: Message.java From android-orm-benchmark-updated with Apache License 2.0 | 6 votes |
public static void createTable(SQLiteOpenHelper helper) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL(new StringBuilder("CREATE TABLE '").append(TABLE_NAME) .append("' ('").append(BaseColumns._ID) .append("' INTEGER PRIMARY KEY AUTOINCREMENT, '") .append(CLIENT_ID).append("' INTEGER, '").append(SORTED_BY) .append("' REAL, '").append(CREATED_AT).append("' INTEGER, '") .append(CONTENT).append("' TEXT, '").append(SENDER_ID) .append("' INTEGER NOT NULL, '").append(CHANNEL_ID) .append("' INTEGER NOT NULL, '").append(COMMAND_ID) .append("' INTEGER);").toString()); db.execSQL(new StringBuilder("CREATE INDEX IDX_MESSAGE_COMMAND_ID ON ") .append(TABLE_NAME).append(" (").append(COMMAND_ID) .append(");").toString()); }
Example 3
Source File: HistoryManager.java From zxingfragmentlib with Apache License 2.0 | 5 votes |
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); } }
Example 4
Source File: HistoryManager.java From android-apps with MIT License | 5 votes |
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); } }
Example 5
Source File: HistoryManager.java From ZXing-Standalone-library with Apache License 2.0 | 5 votes |
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); } }
Example 6
Source File: HistoryManager.java From android-apps with MIT License | 5 votes |
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); } }
Example 7
Source File: HistoryManager.java From ZXing-Standalone-library with Apache License 2.0 | 5 votes |
/** * <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); } }
Example 8
Source File: HistoryManager.java From Study_Android_Demo with Apache License 2.0 | 5 votes |
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); } }
Example 9
Source File: HistoryManager.java From zxingfragmentlib with Apache License 2.0 | 5 votes |
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); } }
Example 10
Source File: DatabaseTest.java From android-dev-challenge with Apache License 2.0 | 5 votes |
/** * Tests to ensure that inserts into your database results in automatically * incrementing row IDs. * @throws Exception in case the constructor hasn't been implemented yet */ @Test public void autoincrement_test() throws Exception{ /* First, let's ensure we have some values in our table initially */ insert_single_record_test(); /* Use reflection to try to run the correct constructor whenever implemented */ SQLiteOpenHelper dbHelper = (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext); /* Use WaitlistDbHelper to get access to a writable database */ SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues testValues = new ContentValues(); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name"); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99); /* Insert ContentValues into database and get first row ID back */ long firstRowId = database.insert( WaitlistContract.WaitlistEntry.TABLE_NAME, null, testValues); /* Insert ContentValues into database and get another row ID back */ long secondRowId = database.insert( WaitlistContract.WaitlistEntry.TABLE_NAME, null, testValues); assertEquals("ID Autoincrement test failed!", firstRowId + 1, secondRowId); }
Example 11
Source File: HistoryManager.java From android-apps with MIT License | 5 votes |
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); } }
Example 12
Source File: HistoryManager.java From incubator-weex-playground with Apache License 2.0 | 5 votes |
/** * <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); } }
Example 13
Source File: HistoryManager.java From Study_Android_Demo with Apache License 2.0 | 5 votes |
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); } }
Example 14
Source File: HistoryManager.java From weex with Apache License 2.0 | 5 votes |
/** * <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); } }
Example 15
Source File: DatabaseTest.java From android-dev-challenge with Apache License 2.0 | 5 votes |
/** * Tests to ensure that inserts into your database results in automatically * incrementing row IDs. * @throws Exception in case the constructor hasn't been implemented yet */ @Test public void autoincrement_test() throws Exception{ /* First, let's ensure we have some values in our table initially */ insert_single_record_test(); /* Use reflection to try to run the correct constructor whenever implemented */ SQLiteOpenHelper dbHelper = (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext); /* Use WaitlistDbHelper to get access to a writable database */ SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues testValues = new ContentValues(); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name"); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99); /* Insert ContentValues into database and get first row ID back */ long firstRowId = database.insert( WaitlistContract.WaitlistEntry.TABLE_NAME, null, testValues); /* Insert ContentValues into database and get another row ID back */ long secondRowId = database.insert( WaitlistContract.WaitlistEntry.TABLE_NAME, null, testValues); assertEquals("ID Autoincrement test failed!", firstRowId + 1, secondRowId); }
Example 16
Source File: HistoryManager.java From analyzer-of-android-for-Apache-Weex with Apache License 2.0 | 5 votes |
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); } }
Example 17
Source File: DatabaseTest.java From android-dev-challenge with Apache License 2.0 | 4 votes |
/** * This method tests inserting a single record into an empty table from a brand new database. * The purpose is to test that the database is working as expected * @throws Exception in case the constructor hasn't been implemented yet */ @Test public void insert_single_record_test() throws Exception{ /* Use reflection to try to run the correct constructor whenever implemented */ SQLiteOpenHelper dbHelper = (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext); /* Use WaitlistDbHelper to get access to a writable database */ SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues testValues = new ContentValues(); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name"); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99); /* Insert ContentValues into database and get first row ID back */ long firstRowId = database.insert( WaitlistContract.WaitlistEntry.TABLE_NAME, null, testValues); /* If the insert fails, database.insert returns -1 */ assertNotEquals("Unable to insert into the database", -1, firstRowId); /* * Query the database and receive a Cursor. A Cursor is the primary way to interact with * a database in Android. */ Cursor wCursor = database.query( /* Name of table on which to perform the query */ WaitlistContract.WaitlistEntry.TABLE_NAME, /* Columns; leaving this null returns every column in the table */ null, /* Optional specification for columns in the "where" clause above */ null, /* Values for "where" clause */ null, /* Columns to group by */ null, /* Columns to filter by row groups */ null, /* Sort order to return in Cursor */ null); /* Cursor.moveToFirst will return false if there are no records returned from your query */ String emptyQueryError = "Error: No Records returned from waitlist query"; assertTrue(emptyQueryError, wCursor.moveToFirst()); /* Close cursor and database */ wCursor.close(); dbHelper.close(); }
Example 18
Source File: DatabaseTest.java From android-dev-challenge with Apache License 2.0 | 4 votes |
/** * This method tests inserting a single record into an empty table from a brand new database. * The purpose is to test that the database is working as expected * @throws Exception in case the constructor hasn't been implemented yet */ @Test public void insert_single_record_test() throws Exception{ /* Use reflection to try to run the correct constructor whenever implemented */ SQLiteOpenHelper dbHelper = (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext); /* Use WaitlistDbHelper to get access to a writable database */ SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues testValues = new ContentValues(); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name"); testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99); /* Insert ContentValues into database and get first row ID back */ long firstRowId = database.insert( WaitlistContract.WaitlistEntry.TABLE_NAME, null, testValues); /* If the insert fails, database.insert returns -1 */ assertNotEquals("Unable to insert into the database", -1, firstRowId); /* * Query the database and receive a Cursor. A Cursor is the primary way to interact with * a database in Android. */ Cursor wCursor = database.query( /* Name of table on which to perform the query */ WaitlistContract.WaitlistEntry.TABLE_NAME, /* Columns; leaving this null returns every column in the table */ null, /* Optional specification for columns in the "where" clause above */ null, /* Values for "where" clause */ null, /* Columns to group by */ null, /* Columns to filter by row groups */ null, /* Sort order to return in Cursor */ null); /* Cursor.moveToFirst will return false if there are no records returned from your query */ String emptyQueryError = "Error: No Records returned from waitlist query"; assertTrue(emptyQueryError, wCursor.moveToFirst()); /* Close cursor and database */ wCursor.close(); dbHelper.close(); }
Example 19
Source File: HistoryManager.java From Study_Android_Demo with Apache License 2.0 | 4 votes |
public void addHistoryItemDetails(String itemID, String itemDetails) { // As we're going to do an update only we don't need need to worry // about the preferences; if the item wasn't saved it won't be udpated SQLiteOpenHelper helper = new DBHelper(activity); SQLiteDatabase db = null; Cursor cursor = null; try { db = helper.getWritableDatabase(); cursor = db.query(DBHelper.TABLE_NAME, ID_DETAIL_COL_PROJECTION, DBHelper.TEXT_COL + "=?", new String[] { itemID }, null, null, DBHelper.TIMESTAMP_COL + " DESC", "1"); String oldID = null; String oldDetails = null; if (cursor.moveToNext()) { oldID = cursor.getString(0); oldDetails = cursor.getString(1); } if (oldID != null) { String newDetails; if (oldDetails == null) { newDetails = itemDetails; } else if (oldDetails.contains(itemDetails)) { newDetails = null; } else { newDetails = oldDetails + " : " + itemDetails; } if (newDetails != null) { ContentValues values = new ContentValues(); values.put(DBHelper.DETAILS_COL, newDetails); db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID }); } } } finally { close(cursor, db); } }
Example 20
Source File: DatabaseTest.java From android-dev-challenge with Apache License 2.0 | 4 votes |
/** * This method tests that our database contains all of the tables that we think it should * contain. * @throws Exception in case the constructor hasn't been implemented yet */ @Test public void create_database_test() throws Exception{ /* Use reflection to try to run the correct constructor whenever implemented */ SQLiteOpenHelper dbHelper = (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext); /* Use WaitlistDbHelper to get access to a writable database */ SQLiteDatabase database = dbHelper.getWritableDatabase(); /* We think the database is open, let's verify that here */ String databaseIsNotOpen = "The database should be open and isn't"; assertEquals(databaseIsNotOpen, true, database.isOpen()); /* This Cursor will contain the names of each table in our database */ Cursor tableNameCursor = database.rawQuery( "SELECT name FROM sqlite_master WHERE type='table' AND name='" + WaitlistContract.WaitlistEntry.TABLE_NAME + "'", null); /* * If tableNameCursor.moveToFirst returns false from this query, it means the database * wasn't created properly. In actuality, it means that your database contains no tables. */ String errorInCreatingDatabase = "Error: This means that the database has not been created correctly"; assertTrue(errorInCreatingDatabase, tableNameCursor.moveToFirst()); /* If this fails, it means that your database doesn't contain the expected table(s) */ assertEquals("Error: Your database was created without the expected tables.", WaitlistContract.WaitlistEntry.TABLE_NAME, tableNameCursor.getString(0)); /* Always close a cursor when you are done with it */ tableNameCursor.close(); }