Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,741,682 Members 41,669 Now Online
XDA Developers Android and Mobile Development Forum

adding an image column to an existing tabe in sqlite database

Tip us?
 
SufiyanSadiq
Old
(Last edited by SufiyanSadiq; 13th March 2014 at 02:34 PM.) Reason: forgot to add something
#1  
Junior Member - OP
Thanks Meter 0
Posts: 7
Join Date: Jan 2014
Location: Hyderabad
Default adding an image column to an existing tabe in sqlite database

I wanted to add a column which will be able to store images, to an existing sqlite table which has two columns, What datatype column do i create so that i can be able to store images in my database?
My code looks like this


public class Sqlite extends SQLiteOpenHelper {

public static final String DB_NAME = "vault";
public static final int DB_VERSION = 1;
public static final String DB_CREATE = "CREATE TABLE data(key VARCHAR(32) PRIMARY KEY NOT NULL, value VARCHAR(100) NOT NULL)";


public SQLiteDatabase db;
public Context context;

public Sqlite(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}

@override
public void onCreate(SQLiteDatabase db) {
this.db = db;
this.db.execSQL(DB_CREATE);
}

@override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

@override
public synchronized void close() {
if(this.db != null) {
this.db.close();
super.close();
}
}

public List<VaultItem> getAll() throws Exception {
// load items
List<VaultItem> items = new ArrayList<VaultItem>();

// query db
Cursor c = db.query("data", new String[]{"key", "value"}, null, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast()) {
items.add(new VaultItem(c.getString(0), SimpleCrypto.decrypt(c.getString(1))));
c.moveToNext();
}
return items;
}

public VaultItem getOne(String key) throws Exception {
Cursor c = db.query("data", new String[]{"key", "value"}, "key = '"+key+"'", null, null, null, null);
c.moveToFirst();
VaultItem item=null;
while(!c.isAfterLast()) {
item = new VaultItem(c.getString(0), SimpleCrypto.decrypt(c.getString(1)));
c.moveToNext();
}
return item;
}
}

This table only stores, .txt documents, with their name and content. I want it to to be able to store .doc documents and .jpg files. Please help me...
 
MattMatt0240
Old
#2  
MattMatt0240's Avatar
Senior Member
Thanks Meter 95
Posts: 210
Join Date: Sep 2011
A Blob data type is what you would want to store in a database for images. http://developer.android.com/referen.../sql/Blob.html has good info on it.

Basically take your image (bitmap) and convert to byte[] to store in db. Retrieve it by converting byte array to bitmap.

Do not forget to update your DB version to update db with new fields.

Noted to self thrice via tapatalk
Root it and boot it!

Samsung Galaxy Note 3 | PAC-ROM

Retired phones: Epic Touch 4G| Evo shift 4g | Evo 4g
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes