Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Can Titanium do a Batch insert into a database

Greetings Titanium coders!

I am trying to perform a bunch of INSERT into a database, following a bunch of SELECT, this within a serie of nested loops.

To my great demise, after a certain number of loops, never the same quantity, the app crashes silently. I tried to run the loops trough a setInterval, which helped a little, but even with a delay of 2 seconds in between the loops, the app will still crash, .

I suspect that there is a weird bug in the sql-lite system, so, I would like to create a batch INSERT in the db, this to avoid a "locked database" crash.

QUESTION : HOW CAN WE PERFORM A BATCH INSERT STATEMENT OR A SERIE OF PREPARED SQL INTO A TITANIUM DATABASE?

Thanks a lot for any help!

// fake code
// myArray.length is large ++1000 !

var done = false;
var myArray = [];

myArray = jsonResponse; //"lots of data from a json http request";

var iterator = 0;

var interval_x = setInterval(function()
{
    if(myArray.length === iterator)
    {
        done = true;
    {


    if(done === false)
    {
        var myDb = Titanium.Database.open('someDatabase');
        for(var item in myArray[iterator])
        {
            var aValue = myArray[item];
            var rows = myDb.execute("SELECT somefield FROM aTable WHERE ID = ?", aValue);
            while(rows.isValidRow())
            {
                var newValue = rows.fieldByName('savedValue');

                // the app will crash here after some intervals
                myDb.execute("REPLACE INTO someOtherTable (newField) VALUES (?)", newValue);
                rows.next();
            }
    }
    else
    {
        removeInterval(interval_x);
    }
    ++iterator;
    myDb.close();

}, 1000);
— asked July 13th 2011 by Luc Martin
  • database batch
  • database in loop
  • prepared statements
  • sqllite crashes
0 Comments

2 Answers

  • Accepted Answer

    First of all, even comparatively small loops (2/3 thousand cycles) will crash in Titanium. I had crashes in XML processing, SQL inserting, filesystem checks, etc. All random, all without expanation. This is annoying and some word from the Appcelerator team would be great.

    Now, yes, you can perform a single command for multiple inserts:

    db.execute("BEGIN IMMEDIATE TRANSACTION");
    //INSERT COMMANDS FROM YOUR LOOP HERE
    db.execute("COMMIT TRANSACTION");
    

    This will not crash. Point is, if you need to select something and then insert, I don't think that's gonna work (never tried but I'm pretty sure that you can't get results between queries this way). My solution would be to select what you need, store it in an array and then do multiple inserts querying your array instead of the database.

    — answered July 13th 2011 by Abe Facciazzi
    permalink
    3 Comments
    • Abe, thanks a lot for the answer! I will test it and get back to you. It may very well work since the problem seem located in the action of writing to file.

      — commented July 13th 2011 by Luc Martin
    • Thanks for the answer. Unfortunately you are right about crashing anyway. I have to delay my loops up to 4000 ms in order to let Titanium "digest" the data. Pretty annoying…

      — commented July 13th 2011 by Luc Martin
    • Abe, Since you are at it, could you be kind enough to write here a simple example of an insert statement prepared for a transaction? Maybe this is what I am doing wrong?

      Thanks again

      — commented July 13th 2011 by Luc Martin
  • This is what I do. In this example, I only insert if each value in the items_array is bigger than 100:

    db.execute("BEGIN IMMEDIATE TRANSACTION");
    for (var d=0;d<items_array.length;d++) {
    var id = items_array[d];
       if(id > 100){
       db.execute('INSERT INTO users (userid) VALUES(?)', id);
       }
    }
    db.execute("COMMIT TRANSACTION");
    
    — answered July 14th 2011 by Abe Facciazzi
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.