@Override public void execSQL(String sql, Object[] bindArgs) throws SQLException { delegate.execSQL(sql, bindArgs); }
public void addEntry( String name, byte[] image) throws SQLiteException{ SQLiteDatabase database = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(KEY_NAME, name); cv.put(KEY_IMAGE, image); database.insert( DB_TABLE, null, cv ); }
public void insertHasRead(String table, String key, int value) { Cursor cursor = mSQLiteDatabase.query(table, null, null, null, null, null, "id asc"); if (cursor.getCount() > 200 && cursor.moveToNext()) { mSQLiteDatabase.delete(table, "id=?", new String[]{String.valueOf(cursor.getInt(cursor.getColumnIndex("id")))}); } cursor.close(); ContentValues contentValues = new ContentValues(); contentValues.put("key", key); contentValues.put("is_read", value); mSQLiteDatabase.insertWithOnConflict(table, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE); }
@Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); if (!db.isReadOnly()) { // Enable foreign key constraints db.execSQL("PRAGMA foreign_keys=ON;"); } }
private void insert(CacheFileInfo cacheFileInfo) { SQLiteDatabase sqLiteDatabase = getWritableDatabase(); sqLiteDatabase.beginTransaction(); try { ContentValues cv = packData(cacheFileInfo); sqLiteDatabase.insert(TABLE_NAME, null, cv); sqLiteDatabase.setTransactionSuccessful(); } finally { sqLiteDatabase.endTransaction(); } }
DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))>0) mOutput.write(mBuffer, 0, mLength); mOutput.flush(); mOutput.close(); mInput.close(); mDataBase.close(); super.close();
OutputStream myOutput = new FileOutputStream(outFileName); while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); myOutput.flush(); myOutput.close(); myInput.close(); public synchronized void close() { if(sqliteDataBase != null) sqliteDataBase.close(); super.close(); Cursor cursor = sqliteDataBase.rawQuery(query, null); String userName = null; if(cursor.getCount()>0){ if(cursor.moveToFirst()){ do{ userName = cursor.getString(0); }while (cursor.moveToNext());
public void writeImage(Bitmap thumbnail, byte[] imageBytes) { try { File imageFile = File.createTempFile("wallpaper", "", mContext.getFilesDir()); FileOutputStream imageFileStream = mContext.openFileOutput(imageFile.getName(), Context.MODE_PRIVATE); imageFileStream.write(imageBytes); imageFileStream.close(); File thumbFile = File.createTempFile("wallpaperthumb", "", mContext.getFilesDir()); FileOutputStream thumbFileStream = mContext.openFileOutput(thumbFile.getName(), Context.MODE_PRIVATE); thumbnail.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream); thumbFileStream.close(); SQLiteDatabase db = mDb.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(ImageDb.COLUMN_IMAGE_THUMBNAIL_FILENAME, thumbFile.getName()); values.put(ImageDb.COLUMN_IMAGE_FILENAME, imageFile.getName()); db.insert(ImageDb.TABLE_NAME, null, values); } catch (IOException e) { Log.e(TAG, "Failed writing images to storage " + e); } }
public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 15; private Context mCtx; //<-- declare a Context reference public DatabaseHandler(Context context) { super(context, "rettinfo", null, DATABASE_VERSION); mCtx = context; //<-- fill it with the Context you are passed } @Override public void onCreate(SQLiteDatabase db) { Log.d("Create: ", "Creating antidotlist"); String CREATE_ANTIDOT_TABLE = "CREATE TABLE antidots (id INTEGER PRIMARY KEY antidot TEXT, dos TEXT)"; Log.d("Create: ", CREATE_ANTIDOT_TABLE); db.execSQL(CREATE_ANTIDOT_TABLE); InputStream antidots = mCtx.getAssets().open("antidot/antidots"); //<-- call getAssets on your Context object. InputStreamReader input = new InputStreamReader(antidots); BufferedReader buffreader = new BufferedReader(input,2*1024); String line; while ((line = buffreader.readLine()) != null) { String[] point_t = line.split(","); } antidots.close(); } }
public boolean importDatabase(String dbPath) throws IOException { close(); File newDb = new File(dbPath); File oldDb = new File(DATABASE_PATH); if (newDb.exists()) { copyFile(new FileInputStream(newDb), new FileOutputStream(oldDb)); getWritableDatabase().close(); return true; } return false; }
private void put(SQLiteDatabase db, String name, Bundle bundle) { ByteArrayOutputStream valueStream = new ByteArrayOutputStream(); try { ContentValues rows = new ContentValues(); rows.put("name", name); Parcel p = Parcel.obtain(); bundle.writeToParcel(p, 0); valueStream.write(p.marshall()); rows.put("value", valueStream.toByteArray()); db.insert("bundles", null, rows); valueStream.close(); } catch (IOException e) { Log.e("error writing object", e.toString()); } }
@Test public void testDataInMemoryDatabaseIsPersistentAfterClose() throws Exception { SQLiteDatabase db1 = openOrCreateDatabase("db1"); db1.execSQL("CREATE TABLE foo(id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);"); ContentValues d1 = new ContentValues(); d1.put("data", "d1"); db1.insert("foo", null, d1); db1.close(); SQLiteDatabase db2 = openOrCreateDatabase("db1"); Cursor c = db2.rawQuery("select * from foo", null); assertThat(c).isNotNull(); assertThat(c.getCount()).isEqualTo(1); assertThat(c.moveToNext()).isTrue(); assertThat(c.getString(c.getColumnIndex("data"))).isEqualTo("d1"); }
@Test public void shouldAlwaysReturnCorrectIdFromInsert() throws Exception { database.execSQL("CREATE TABLE table_A (\n" + " _id INTEGER PRIMARY KEY AUTOINCREMENT,\n" + " id INTEGER DEFAULT 0\n" + ");"); database.execSQL("CREATE VIRTUAL TABLE new_search USING fts3 (id);"); database.execSQL("CREATE TRIGGER t1 AFTER INSERT ON table_A WHEN new.id=0 BEGIN UPDATE table_A SET id=-new._id WHERE _id=new._id AND id=0; END;"); database.execSQL("CREATE TRIGGER t2 AFTER INSERT ON table_A BEGIN INSERT INTO new_search (id) VALUES (new._id); END;"); database.execSQL("CREATE TRIGGER t3 BEFORE UPDATE ON table_A BEGIN DELETE FROM new_search WHERE id MATCH old._id; END;"); database.execSQL("CREATE TRIGGER t4 AFTER UPDATE ON table_A BEGIN INSERT INTO new_search (id) VALUES (new._id); END;"); long[] returnedIds = new long[]{ database.insert("table_A", "id", new ContentValues()), database.insert("table_A", "id", new ContentValues()) }; Cursor c = database.query("table_A", new String[]{"_id"}, null, null, null, null, null); assertThat(c).isNotNull(); long[] actualIds = new long[c.getCount()]; for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { actualIds[c.getPosition()] = c.getLong(c.getColumnIndexOrThrow("_id")); } c.close(); assertThat(returnedIds).isEqualTo(actualIds); }
@Test public void testSuccessNestedTransaction() throws Exception { database.beginTransaction(); database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');"); database.beginTransaction(); database.execSQL("INSERT INTO table_name (id, name) VALUES(12345, 'Julie');"); database.setTransactionSuccessful(); database.endTransaction(); database.setTransactionSuccessful(); database.endTransaction(); Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(2); }
@Test public void shouldCorrectlyReturnNullValues() { database.execSQL("CREATE TABLE null_test (col_int INTEGER, col_text TEXT, col_real REAL, col_blob BLOB)"); ContentValues data = new ContentValues(); data.putNull("col_int"); data.putNull("col_text"); data.putNull("col_real"); data.putNull("col_blob"); assertThat(database.insert("null_test", null, data)).isAtLeast(0L); Cursor nullValuesCursor = database.query("null_test", null, null, null, null, null, null); nullValuesCursor.moveToFirst(); final int colsCount = 4; for (int i = 0; i < colsCount; i++) { assertThat(nullValuesCursor.getType(i)).isEqualTo(Cursor.FIELD_TYPE_NULL); assertThat(nullValuesCursor.getString(i)).isNull(); } assertThat(nullValuesCursor.getBlob(3)).isNull(); }
@Test public void testInsertAndQuery() throws Exception { String stringColumnValue = "column_value"; byte[] byteColumnValue = new byte[]{1, 2, 3}; ContentValues values = new ContentValues(); values.put("first_column", stringColumnValue); values.put("second_column", byteColumnValue); database.insert("table_name", null, values); Cursor cursor = database.query("table_name", new String[]{"second_column", "first_column"}, null, null, null, null, null); assertThat(cursor.moveToFirst()).isTrue(); byte[] byteValueFromDatabase = cursor.getBlob(0); String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); }
@Test public void testInsertAndRawQuery() throws Exception { String stringColumnValue = "column_value"; byte[] byteColumnValue = new byte[]{1, 2, 3}; ContentValues values = new ContentValues(); values.put("first_column", stringColumnValue); values.put("second_column", byteColumnValue); database.insert("table_name", null, values); Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null); assertThat(cursor.moveToFirst()).isTrue(); byte[] byteValueFromDatabase = cursor.getBlob(0); String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); }
@Test public void testFailureTransaction() throws Exception { database.beginTransaction(); database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');"); final String select = "SELECT COUNT(*) FROM table_name"; Cursor cursor = database.rawQuery(select, null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(1); cursor.close(); database.endTransaction(); cursor = database.rawQuery(select, null); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getInt(0)).isEqualTo(0); }