Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
rakesh86shankar
Posts: 1,003
Registered: ‎05-22-2009
My Device: Not Specified

How to write and read a blob object in Blackberrry Java

Hi,

 

I want to store my Object which is mms object to be stored in Db and retreive it back programatically,Is there any way to achieve this in Blackberry java?

 

Thanks

Rakesh

Please use plain text.
Developer
rakesh86shankar
Posts: 1,003
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

I am trying to save my object as blob object in sqlite  ,should i save it as bytes and retreive it?

 

IS there any way to do it?

 

 

Thanks

Rakesh

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

Not as far as I am aware - I think you are going to have to serialise the object into bytes for write and recreate it on read. 

Please use plain text.
Developer
rakesh86shankar
Posts: 1,003
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

Can you ellaborate on this?

 

I have MMS Object which should  be serialized ,Is there any sample codes for reference.

 

Thanks

Rakesh

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

Because there is no reflection in the level of Java on the BB, you have to do the serialization yourself.  You will have to work through the attributes of the Object and create a byte form of each.  You will create this with markers so that you can interpret the byte data when you read it back.  You will write out the byte form, and then reprocess the byte form on read, to recreate the attributes. 

 

Hopefully this is clear enough.

 

I suggest you use JSON as the format of your stream.  Then in fact you will only have to write the processing that creates the Object, you can just use the JSON parser to process the input and recreate your Object from that. 

 

Sorry I don't have a sample that I can give you. 

 

You will find plenty of information on serializing J2ME Objects using your favorite search engine. 

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

Please use plain text.
Developer
rakesh86shankar
Posts: 1,003
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

[ Edited ]

Hi Peter,

 

 

As suggested by you,for the purpose of serialisation i converted in JSON Object and i inserted in the table as below

 

d.executeStatement( "INSERT INTO MyTable1(MessageBody) VALUES( ZEROBLOB( 1024 ) );" );		
			java.io.OutputStream outputStream =  d.createBlobOutputStream( "MyTable1", "MessageBody", d.lastInsertedRowID( ) );
			outputStream.write(MessageBody.toString().getBytes("UTF-8"));	
			d.close();

 Similarly while i am retreiving data from table,I am facing issue such as stream closed and i code is below

 

public void fetchfromDb() throws DatabaseIOException{
		Database d = null;
		Statement st;
		String queryStatement = "SELECT * FROM Mytable1" ;
		try {
			URI myURI = URI.create(DATABASE_PATH + DATABASE_NAME);
			d = DatabaseFactory.open(myURI);
			st = d.createStatement(queryStatement);//d.executeStatement( "SELECT * FROM Mytable1" );		
			st.prepare();
			net.rim.device.api.database.Cursor c = st.getCursor();
			Vector list = new Vector();
			Row r;
			int i = 0;
			ByteArrayOutputStream byteArray=new ByteArrayOutputStream();
			System.out.println("C Value is >>>>>"+c.getPosition());
		
			while (c.next()) {
					System.out.println("Row Count >>>>> "+i);
					Row row = c.getRow();					
					i++;
					InputStream is= c.getRow().getBlobStream(i);
					//java.io.InputStream inputStream = d.createBlobInputStream( "Mytable1", "MessageBody", i );//It throws error
				 	byte[] buffer = new byte[2000];
			        	int bytesRead = is.read(buffer);
			        	while(bytesRead > 0) {
			        	 byteArray.write(buffer, 0, bytesRead);
			             	 bytesRead = is.read(buffer);//While reading or looping happens second time ,i am getting issue InputStream closed Exception 
			        	}
			               	String Response = new String(byteArray.toByteArray(), "UTF-8");
			        	JSONObject obj = new JSONObject(Response);			        
			    		Logger.LogInfo("Retreived Data frm Db", "JSON Response --> " + obj.toString());
			    		byteArray.close();
			       		is.close();
			}		
			
		}catch (Exception e) {
			System.out.println("Exception in Retreiving Blob Object>>>>>>"+e.getMessage());
		}finally{
			d.close();
		}
		
	}

 Am i Doing it write way,Should i make is null at the end of the loop,what is problem in my code,My code breaks showing exception,InputStream (is) is closed,After the first time execution of while loop.

 

Thanks

Rakesh

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

Sorry not sure.  Here are some things for you to look at:

 

A) Have you used any SQLite utilities to see if the data correctly gets into your database>

 

There are two oddities in your code

 

B.1) What is your index 'i' doing?  To use getBlob..(int columnIndex) methods you supply the column index.  That does not seem to be what you are supplying as a parameter.

 

B.2) Why are you not using getBlobBytes(..) to get the bytes?

 

I have a few other comments:

 

C) You say that you are writing a JSON formatted String.  Are you sure this is what you are doing.  I suggest you print out the String that you are putting into the database and make sure it is actually JSON.

 

D) I haven't used a Blob myself but it seems that you are restricting your blob to being 1024 bytes long.  Is that what you intended? 

 

One other comment, in future can I suggest you stop using the code you have for reading a stream of bytes,  Instead use the more efficient built-in utility IOUtilities.streamToBytes(..) to for this for you.  You don't need to use it in this code because you can use getBlobBytes(..); 

Please use plain text.
Developer
rakesh86shankar
Posts: 1,003
Registered: ‎05-22-2009
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

Hi Peter,

 

Thanks for your replies ,

 

a)I am using sqlite manager, a firefox add on to check Db

 

b)i)Yes I am wrong here,which is cause of whole issue,if i use 0 it works :smileyhappy:

 

ii)Yes I am using same way as suggested.

 

c)yes I checked it is a JSON Format,I put a SOP onsave and retrieve from DB.

 

d)Yes i changed size to 1024*400 (for 400 KB)

 

 

Finally sadest part is ,issue was fixed as suggested by you on lookin b i)

 

 

It works on 7.0 device.This API are not available on 6.0 devices.How do i save Object of size 400KB,in SQlite ?

 

It there is no other way on SQLite,Can we do it in persistance memory,(64 KB)But has it has reliability on flash memory.

 

Is there any other way of Saving 400 Kb object ?

 

 

Thanks

Rakesh

 

Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to write and read a blob object in SQLITE

[ Edited ]

1) "Finally sadest part is ,issue was fixed as suggested by you on lookin b i)"

 

Why saddest?

 

2) "It works on 7.0 device.This API are not available on 6.0 devices.How do i save Object of size 400KB,in SQlite ?"

 

Which API is not available in OS 6.0?

 

3) "Can we do it in persistance memory"

 

Why not?

 

4) "(64 KB)"

 

What do you mean by this?

 

6) "But has it has reliability on flash memory"

 

What do you mean by this?

 

7) "Is there any other way of Saving 400 Kb object ?"

 

Of course - use a file on SD Card or use persistent store.  Search for persistent on this forum for KB articles and other information that will help you. 

 

It would seem to me that if you have one large Object that you need to save, then SQLite is probably the wrong place to save it.  I would use probably just use persistentStore, or write it out to a single file on the SD Card. 

 

BTW, I recommend using the Spell Check button - using it may save confusion with misspell words. 

Please use plain text.