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

  • SEARCH
  • Home
  • 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 8803205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:16:55+00:00 2026-06-14T01:16:55+00:00

I am trying to create a simple page that takes a binary file and

  • 0

I am trying to create a simple page that takes a binary file and inserts the values into a Web SQL database.
This is the function I am using to insert the data:

function bin2dbfunc()
{
    var result, n, aByte, byteStr;
    var i=0;
    var sql = new Array();

    result = fr.result;  //Input file

    for (n = 0; n < result.length; ++n) 
    {
        aByte = result.charCodeAt(n);
        byteStr = aByte.toString(16);
        if (byteStr.length < 2)
        {
            byteStr = "0" + byteStr;
        } //Format to add leading 0 for hex values

        //Looping through taking each byte read from file and adding to array

        //sql[i] = aByte;   //Value
        sql[i] = byteStr;   //String


        //When completed one row of database run single SQL insert statement with array contents
        if(i==15)
        {
            i=0;  //Clear counter for next row
            db.transaction(function (tx)
            {
                tx.executeSql('INSERT INTO binary_data VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [sql[0], sql[1], sql[2], sql[3], sql[4], sql[5], sql[6], sql[7], sql[8], sql[9], sql[10], sql[11], sql[12], sql[13], sql[14], sql[15]]);
            }, function (tx, err) {
                document.getElementById("result3").innerHTML += 'ERROR ';  //Display error message if SQL not run successfully
            });
        }
        else
        {
            i++;  //Otherwise increment counter
        }
    }
}

I have stripped back the code to remove all my debug messages but essentially the code appears to run. I am using a binary file with 6 rows worth of data, the code however inserts the last row of data 6 times into the database.
Can anyone spot where I am going wrong?

  • 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-06-14T01:16:56+00:00Added an answer on June 14, 2026 at 1:16 am

    Variables are captured by reference and not by value in JavaScript closures (this is true for objects such as arrays but for primitive values as well); this means that the functions passed to db.transaction will use the current value of sql when they run. It seems that they run asynchronously in your case, thus using the value of sql once bin2dbfunc has returned (which explains why you end up with the last row of data).

    You have to copy the value of sql in order to make sure that the right one is used:

    function transactionCallback(sql_) {
      var sql = sql_.slice(0); // copy
      return function(tx) {
        tx.executeSql(…, sql);
      };
    }
    
    db.transaction(transactionCallback(sql), …);
    

    or

    function transactionCallback(sql) {
      return function(tx) {
        tx.executeSql(…, sql);
      };
    }
    
    db.transaction(transactionCallback(sql), …);
    sql = [];
    

    as you don’t need sql‘s values afterwards in bin2dbfunc; this second solution would also enable you to get rid of i and use a more elegant solution based on push and length.

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

Sidebar

Related Questions

I'm trying to create a simple web page that resembles the following: I've got
I am trying to create a simple about page that uses JQuery click to
I'm trying to create a simple web bot which will access a certain page,
I am trying to create a simple page that enters data in to a
I'm trying to create a simple search page, but I'm not 100% sure how
I'm trying to create a simple portfolio page. I have a list of thumbs
I have am trying to create a simple voting page. There are a few
I'm trying to create a simple sinatra app with one page containing a text
I'm trying to create simple script that subscribes a user to a company calendar.
I am trying to create a webservice that takes data, posts it to a

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.