Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:30:39+00:00 2026-05-26T11:30:39+00:00

I currently have a transaction that attempts to insert data into a table. If

  • 0

I currently have a transaction that attempts to insert data into a table. If the data is already in the table a constraint failed error is raised and a select is run to get 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 want to speed the transaction up so I thought I would see if the data was in the table first. If it isn’t then insert 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;
  }
);

…but it doesn’t work. This transaction runs all the selects then does the inserts. How can I make the second method work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:30:40+00:00Added an answer on May 26, 2026 at 11:30 am

    I’m assuming the transaction is queuing the request. So your queue will look like this

    Select 1

    Select 2

    Select 3

    Then when you commit your transaction looks like this after the first call.

    Select 2

    Select 3

    Insert 1

    Insert 2

    Insert 3

    This is happening because the the functions to call the inserts are exected after the select is ran and this doesn’t happen until the transaction commits but the selects have already be registered.

    In order to get it to be

    select1

    insert1

    select2

    insert2

    I would create a separate transaction for each select statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long running insert transaction that inserts data into several related tables.
I currently have a SQL Server (Express 2005) database to hold some transaction/metadata that
I currently have an MS Access application that connects to a PostgreSQL database via
I have a SQL query that I'm currently solving by doing two queries. I
We have a SQL Server database table that consists of user id, some numeric
I have Transaction objects that are combined in a Cart object. Some Transaction objects
I currently have a large form that gets sent to our payment authorizer (done
Currently I have a large import process that I'm trying to wrap inside a
I currently have a directory with 98,000 individual archive transaction files. I need to
I have two queries that are basically the same: OLD TRANSACTIONS QUERY SELECT t.payment_method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.