Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

adding an image column to an existing tabe in sqlite database

OP SufiyanSadiq

13th March 2014, 03:29 PM   |  #1  
OP Junior Member
Flag Hyderabad
Thanks Meter: 0
 
7 posts
Join Date:Joined: Jan 2014
More
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...
Last edited by SufiyanSadiq; 13th March 2014 at 03:34 PM. Reason: forgot to add something
6th April 2014, 02:00 AM   |  #2  
MattMatt0240's Avatar
Senior Member
Thanks Meter: 105
 
244 posts
Join Date:Joined: Sep 2011
More
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
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes