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
Contributor
handyway2011
Posts: 35
Registered: ‎02-02-2012
My Device: 9300
My Carrier: Vodafone

Re: App runs slowly with JSON

Thanks maadani for your help!
I've tryed what you said me, and now my code looks like this:

int l=json.getLenght();
JSONObject jObj=new JSONObject();
       
st = db.createStatement("INSERT INTO Dendak(ID,Izena,Helbidea,Telefonoa,Emaila,Mota,Produktuak,Ordutegia,Horario,Latitud,Longitud,Argazkia,Web,Txartela,Berezitasunak,Especialidades) VALUES (?,?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?)");
       st.prepare();
       
       for (int i=0; i<l;i++)
       { 
        jObj=json.getJSONObject(i);
        st.bind(1, jObj.getInt("id"));
        st.bind(2, jObj.getString("izena"));
        st.bind(3, jObj.getString("helbidea"));
        st.bind(4,jObj.getString("telefonoa"));
        st.bind(5,jObj.getString("emaila"));        
        st.bind(6, jObj.getInt("mota"));
        st.bind(7, jObj.getString("produktuak"));
        st.bind(8,jObj.getString("ordutegia"));
        st.bind(9, jObj.getString("horario"));
        st.bind(10,jObj.getInt("latitud"));
        st.bind(11, jObj.getInt("longitud"));
        st.bind(12, jObj.getString("argazkia"));
        st.bind(13, jObj.getString("web"));
        st.bind(14, jObj.getInt("txartela"));
        st.bind(15, jObj.getString("berezitasuna"));
        st.bind(16, jObj.getString("especialidad"));
        
        st.execute();
        st.reset();        
        
       }
       
       st.close();

 

Json is an JsonArray that I get from my web service, and has more or less 200 objects.

I run this code, and I need more or less 3 minutes for entering all to my database.

 

Any idea of how to do it faster?

I've done something nearly same for Android and runs much faster....

Please use plain text.
Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: App runs slowly with JSON

I think like any database, you can gain performance by optimising your commits. Have you tried wrapping in a transaction then experimenting with the optimum batch size for commits.

 

I not also you are writing to an SD card, is there an issue with the card?

Chris

Please use plain text.
Contributor
handyway2011
Posts: 35
Registered: ‎02-02-2012
My Device: 9300
My Carrier: Vodafone

SQLite slow insert

[ Edited ]

Thank yor Chris for your answer.
I have not tryied wrappung in a transaction, because I don't find how to do that... Any documentation?
I'm writing to an SD card and I don't have any issues with the card!

Please use plain text.
Developer
crispyoz
Posts: 223
Registered: ‎10-01-2011
My Device: Bold 9900

Re: SQLite slow insert

Like this:

 

int count = 0;

final int batchSize = 20; // number records to process before commiting the transaction

boolean dirty = false;

 

while (have more record) {

   db.beginTransaction();

 

    // execute your insert statement 

  st.prepare...

  st.execute ...

    

    // see if we have processed the same number of records in our batch size  

    if (count % batchSize ==0){    

         db.commitTransaction();

         dirty = false;

    } else

        dirty = true;

}

 

if (dirty)  // commit any records that are in the final incomplete batch

       db.commitTransaction();

 

 

I would also suggest you compare how it performs if you write to anywhere other then the SD card, just to take that factor our of the equation.

 

Chris

Please use plain text.