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
Posts: 406
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer
Accepted Solution

Insert values to table dynamically

I want to pass string to a method,where the method does inserting values into the database table,how can i achieve it

public void insertData(int cid,String ttl,String folderName){
    	try
    	{
     uri = URI.create("file:///SDCard/documents/database1.db");
    	d = DatabaseFactory.open(uri);
    	Statement st = d.createStatement("INSERT INTO app_detail(CID,TITLE,FOLDERNAME) " +
    	"VALUES (cid,ttl,folderName)");
    	st.prepare();
    	st.execute();
    	st.close();
    	d.close();
    }
    	catch ( Exception e ) {         
		    System.out.println( e.getMessage() );
		    e.printStackTrace();
		}
    }

 I am getting excepting if i pass dynamic values,whereas if i put hard coded values like this

Statement st = d.createStatement("INSERT INTO app_detail(CID,TITLE,FOLDERNAME) " +
    	"VALUES (25,''title','fname')");

 it works.Please help

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Insert values to table dynamically

To understand this problem, I suggest you change this line:

 

    	Statement st = d.createStatement("INSERT INTO app_detail(CID,TITLE,FOLDERNAME) " +
    	"VALUES (cid,ttl,folderName)");

 Into two lines:

 

String sqlStatement = "INSERT INTO app_detail(CID,TITLE,FOLDERNAME) " +
    	"VALUES (cid,ttl,folderName)");
Statement st = d.createStatement(sqlStatement)

 

Now as you can see, you actually have to convert the values into a String, in order to put them into the statement that SQL will execute.  So you need to make sure that you correctly convert the dynamic values to String and append them to the sqlStatement String.

 

There is usually a pretty straightforward way to convert a variable to a String - I suggest you just search for these.  For example:

 

int i = 0;

String iString = Integer.toString(i);

 

Developer
Posts: 406
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Insert values to table dynamically

@Peter my cid atttribute is an Integer,so i dont think there is any need to convert it to String and the other two are VARCHAR so string should suffice,which i am already passing

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Insert values to table dynamically

Sorry I don't understand your comment. 

 

The statement parameter given to SQL in the createStatement method is a String, and must include your values.  So how can you NOT convert your variables into a String form.  The actual column format is not relevant.

Developer
Posts: 406
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Insert values to table dynamically

It was actually a parametarized insert and i have fixed it in this way,incase anyone having similar issue,below code can be useful 

 

uri = URI.create("file:///SDCard/documents/databsae1.db");
			d = DatabaseFactory.open(uri);
			Statement s = d
					.createStatement("INSERT INTO app_detail(CID,TITLE,FOLDERNAME) "
							+ "VALUES (?, ?, ?)");
			s.prepare();
			s.bind(1, cid);
			s.bind(2, ttl);
			s.bind(3, folderName);
			s.execute();
			s.close();
			d.close();

 

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Insert values to table dynamically

[ Edited ]

Good point, that works.  My comment above was directed at putting the values in the createStatement String.  You could do that too, but you would have to convert the values to a String form. 

 

Solved? Then perhaps you can mark your previous post as the solution?

Regular Contributor
Posts: 50
Registered: ‎03-27-2014
My Device: Z10
My Carrier: Docomo

Re: Insert values to table dynamically

Is it possible to store image path in a table and retrieve that image in a table dynamically?

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Insert values to table dynamically

If you have a new question, then please start a new Thread.

 

You can store camera images in SQL tables.  I have never done it but I understand that they are typically stored as Blobs.