I presently possess a transaction that tries to place data right into a table. When the information is already within the table a constraint unsuccessful error is elevated along with a choose operates to obtain the ID.

t2.executeSql('INSERT INTO books (book) VALUES (?);',
  [record],
  function (t2, r) {        // SQL_successfulCallback
    record = r.insertId;
  },
  function (t2, err) {      // SQL_errorCallback
    if (err.message !== 'constraint failed') { // insert failed because of other
                                               // reason - fail transaction
      console.log('Insert SQL error ' + err.code + ' - ' + err.message + '.');
      return true;
    } else { // insert failed because data was already in the table
      t2.executeSql('SELECT bookID FROM books WHERE book=?',
        [record],
        function (t, r) {   // SQL_successfulCallback
          record = r.rows.item(0).classificationID;
        },
        function (t, err) { // SQL_errorCallback
          console.log('Lookup SQL error ' + err.code + ' - ' + err.message + '.');
          return true;
        }
      );
      return false;
    }
  }
);

I wish to speed the transaction up and so i thought I'd find out if the information is at the table first. Whether it is not then place it...

t2.executeSql('SELECT bookID FROM books WHERE book=?',
  [record],
  function (t2, r) {          // SQL_successfulCallback
    if (r.rows.length !== 0) {
      record = r.rows.item(0).bookID;
    } else {
      t2.executeSql('INSERT INTO books (book) VALUES (?);',
        [record],
        function(t2, r){      // SQL_successfulCallbac
          record = r.insertId;
        },
        function (t2, err) {  // SQL_errorCallback
          if (err.message !== 'constraint failed') { // insert failed because of other
                                                     // reason - fail transaction
            console.log('Insert SQL error ' + err.code + ' - ' + err.message + '.');
            return true;
          } else { // insert failed because data was already in the table
            return false;
          }
        }
      );
    }
  },
  function (t, err) {         // SQL_errorCallback
    console.log('Lookup SQL error ' + err.code + ' - ' + err.message + '.');
    return true;
  }
);

...however it does not work. This transaction runs all of the chooses then does the card inserts. How do i result in the second method work?

I am presuming the transaction is queuing the request. So that your queue may be like this

Choose 1

Choose 2

Choose 3

When you commit your transaction appears like this following the first call.

Choose 2

Choose 3

Place 1

Place 2

Place 3

This really is happening since the the functions to call the card inserts are exected following the choose is went which does not happen before the transaction commits however the chooses have previously be registered.

To be able to get so that it is

select1

insert1

select2

insert2

I'd produce a separate transaction for every choose statement.