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
Highlighted
New Developer
Posts: 47
Registered: ‎05-11-2012
My Device: Blackberry Bold 9930
My Carrier: Tmobile

Sqllite problem!

[ Edited ]

Hi,

i'm working on an app that retrieve data from a json service. In my app I should parse the data into a database and save it in four tables. I already parsed the data and I was successfully able to create the database and add the first and the second tables.

The problem that I'm facing right now is, the second table in my data base keep expanding. I checked the database in the sql browser and I discovered that everytime I click on the app icon it adds the 700 rows to the table again.(ex. 700 becomes 1400) .

(only to the second table, the first table works so fine).

 

Thank you in advance

 

This is my code:

public

voidparseJSONResponceInBB(String jsonInStrFormat)

{

try{

JSONObject json =

newJSONObject(jsonInStrFormat);

JSONArray jArray = json.getJSONArray(

"tables");

 

for(inti=0;i<jArray.length();i++)

{

//Iterate through json array

 

JSONObject j = jArray.getJSONObject(i);

if (j.has("Managers")) {

add(

new LabelField("Managers has been added to the database"));

 

JSONArray j2 = j.getJSONArray(

"Managers");

 

for (intk = 0; k < j2.length(); ++k)

{

JSONObject MangersDetails = j2.getJSONObject(k);

if (MangersDetails.has("fName")) {

 

try

{

URI myURI = URI.create(

"file:///SDCard/Databases/SQLite_Guide/"+

 

"MyTestDatabase.db");

 

d = DatabaseFactory.openOrCreate(myURI);

Statement st =

d.createStatement( "CREATE TABLE Managers ( "+

 

"fName TEXT, "+

 

"lName TEXT, "+

 

"ID TEXT,"+

 

"Type TEXT )");

st.prepare();

st.execute();

st.close();

d.close();

}

catch( Exception e )

{

System.

out.println( e.getMessage() );

e.printStackTrace();

}

try

{

URI myURI = URI.create(

"file:///SDCard/Databases/SQLite_Guide/"+

 

"MyTestDatabase.db");

 

d = DatabaseFactory.open(myURI);

 

Statement st =

d.createStatement("INSERT INTO Managers(fName, lName, ID, Type) "+

 

"VALUES (?,?,?,?)");

 

 

 

st.prepare();

for(intx=0; x <j2.length(); x++){

 

JSONObject F = j2.getJSONObject(x);

//add(new LabelField ("f"));

st.bind(1, F.getString(

"fName"));

st.bind(2, F.getString(

"lName"));

st.bind(3, F.getString(

"ID"));

st.bind(4, F.getString(

"Type"));

st.execute();

st.reset();

}

d.close();

 

 

 

 

 

}

catch( Exception e ) {

System.

out.println( e.getMessage() );

e.printStackTrace();

}

}

 

}

}

 

 

 

}}

catch(JSONException e)

{

e.printStackTrace();

}

 

}

//Owners method

public voidparseJSONResponceInBB1(String jsonInStrFormat)

{

try{

JSONObject json =

newJSONObject(jsonInStrFormat);

JSONArray jArray = json.getJSONArray(

"tables");

 

for(inti=0;i<jArray.length();i++)

{

//Iterate through json array

 

JSONObject j = jArray.getJSONObject(i);

if (j.has("Owners")) {

add(

new LabelField("Owners has been added to the database"));

 

JSONArray j2 = j.getJSONArray(

"Owners");

 

for (intk = 0; k < j2.length(); ++k)

{

JSONObject OwnersDetails = j2.getJSONObject(k);

if(OwnersDetails.has("fName")) {

 

try

{

 

 

Statement st =

d.createStatement( "CREATE TABLE Owners ( "+

 

"fName TEXT, "+

 

"lName TEXT, "+

 

"ID TEXT,"+

 

"Type TEXT )");

st.prepare();

st.execute();

st.close();

d.close();

}

catch( Exception e )

{

 

System.

out.println( e.getMessage() );

e.printStackTrace();

 

}

try

{

 

 

Statement st =

d.createStatement("INSERT INTO Owners(fName, lName, ID, Type) "+

 

"VALUES (?,?,?,?)");

 

 

st.prepare();

for(intx=0; x <j2.length(); x++){

 

JSONObject F = j2.getJSONObject(x);

//add(new LabelField ("f"));

st.bind(1, F.getString(

"fName"));

st.bind(2, F.getString(

"lName"));

st.bind(3, F.getString(

"ID"));

st.bind(4, F.getString(

"Type"));

st.execute();

st.reset();

 

}

d.close();

 

}

catch( Exception e ) {

System.

out.println( e.getMessage() );

e.printStackTrace();

}

}

 

}

}

 

 

 

}}

catch(JSONException e)

{

e.printStackTrace();

}

} }

 

 

Thank you