Last active
December 20, 2015 12:38
-
-
Save kimukou/6132222 to your computer and use it in GitHub Desktop.
sqlite blob test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IrofData implements Serializable{ | |
String name; | |
String twitter_id; | |
String icon_url; | |
} | |
public IrofData getIrof(long key) { | |
if(key==-1)return null; | |
ByteArrayInputStream bais = null; | |
ObjectInputStream oin = null; | |
IrofDB irofDB = new IrofDB(this); | |
try { | |
byte[] bytes = irofDB.getObj(key); | |
if(bytes==null)return null; | |
bais = new ByteArrayInputStream(bytes); | |
oin = new ObjectInputStream(bais); | |
IrofData object = (IrofData)oin.readObject(); | |
return object; | |
} | |
catch (Exception e) { | |
Log.e(TAG,"getIrof",e); | |
return null; | |
} | |
finally{ | |
try { | |
irofDB.close(); | |
if(oin!=null)oin.close(); | |
if(bais!=null)bais.close(); | |
} | |
catch (Exception e) {} | |
} | |
} | |
private boolean updateIrof(long key,IrofData obj) { | |
if(obj==null)return false; | |
ByteArrayOutputStream baos= null; | |
ObjectOutputStream oos = null; | |
IrofDB irofDB = new IrofDB(this); | |
try { | |
baos= new ByteArrayOutputStream(); | |
oos = new ObjectOutputStream(baos); | |
oos.writeObject(obj); | |
byte[] bytes = baos.toByteArray(); | |
ContentValues cv = new ContentValues(); | |
cv.put(DataColumns._OBJ, bytes); | |
irofDB.update(key, cv, null, null); | |
} | |
catch (Exception e) { | |
Log.e(TAG,"updateIrof",e); | |
return false; | |
} | |
finally{ | |
try { | |
irofDB.close(); | |
if(oos!=null)oos.close(); | |
if(baos!=null)baos.close(); | |
} | |
catch (Exception e) {} | |
} | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IrofDB { | |
public interface DataColumns extends BaseColumns { | |
public static final String _ID = "_id"; | |
public static final String _DATE = "_date_time"; | |
public static final String _OBJ = "_instance"; | |
} | |
private SQLiteDatabase irofDB; | |
private static final String DATABASE_TABLE = "irof_instance"; | |
public static class DatabaseHelper extends SQLiteOpenHelper { | |
private static final String DATABASE_NAME = "IrofDB"; | |
public static final int DATABASE_VERSION = 1; | |
private static final String DATABASE_CREATE = | |
new StringBuilder("create table ").append(DATABASE_TABLE) | |
.append(" (") | |
.append(DataColumns._ID).append(" integer primary key autoincrement, ") | |
.append(DataColumns._DATE).append(" text not null") | |
.append(DataColumns._OBJ).append(" blob") | |
.append(");") | |
.toString(); | |
public DatabaseHelper(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(DATABASE_CREATE); | |
} | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE + ";"); | |
} | |
} | |
public IrofDB(Context context) { | |
DatabaseHelper dbHelper = new DatabaseHelper(context); | |
irofDB = dbHelper.getWritableDatabase(); | |
} | |
public IrofDB(SQLiteDatabase database) { | |
irofDB = database; | |
} | |
public void close() { | |
irofDB.close(); | |
} | |
public long insert(ContentValues values) { | |
long rowID = irofDB.insert(DATABASE_TABLE, "", values); | |
if (rowID > 0) return rowID; | |
throw new SQLException("Failed Insert"); | |
} | |
public int delete(long id, String selection, String[] selectionArgs) { | |
return irofDB.delete( | |
DATABASE_TABLE, | |
new StringBuilder(DataColumns._ID).append(" = ").append(id). | |
append(!TextUtils.isEmpty(selection) ? " AND ("+ selection + ')': "").toString(), | |
selectionArgs); | |
} | |
public int clear() { | |
return irofDB.delete(DATABASE_TABLE, "", null); | |
} | |
public Cursor query( | |
String[] projection, | |
String selection, | |
String[] selectionArgs, | |
String sortOrder | |
) { | |
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); | |
sqlBuilder.setTables(DATABASE_TABLE); | |
if (sortOrder == null || sortOrder == "") sortOrder = DataColumns._DATE; | |
return sqlBuilder.query(irofDB, projection, selection, selectionArgs, null, null, sortOrder); | |
} | |
public int update( | |
long id, | |
ContentValues values, | |
String selection, | |
String[] selectionArgs | |
) { | |
return irofDB.update( | |
DATABASE_TABLE, values, | |
new StringBuilder(DataColumns._ID).append(" = ").append(id). | |
append(!TextUtils.isEmpty(selection) ? " AND ("+ selection + ')': "").toString(), | |
selectionArgs); | |
} | |
public byte[] getObj(long id) { | |
Cursor c = scoreDB.rawQuery( | |
"select " + DataColumns._OBJ + " from " + DATABASE_TABLE + | |
" where " + DataColumns._ID + " == ?;" | |
,new String[]{String.valueOf(id)}); | |
if (c.moveToFirst()) { | |
return c.getBlob(0); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment