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 7534581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:03:45+00:00 2026-05-30T06:03:45+00:00

I am working on a piece of code that writes data from 4 different

  • 0

I am working on a piece of code that writes data from 4 different .each blocks into 4 different MySQL rows in a table.

Edit: I should note that these four blocks are nested in one loop. I’ve figured out that it is stacking each db.query on top of the one before because it is generating a new ID for each of them. So, the code IS working…just not as intended. Basically, one row in the table should contain a column and corresponding data for a, b, c, and d. How can I use multiple blocks like this and make sure they’re all writing to the same row?

The code looks a bit like this…

1.upto(5) do |num|
    a.each do |a|
      puts a
      db.query("INSERT INTO Table(Column1) VALUES('#{a}')")
    end

    b.each do |b|
      puts b
     db.query("INSERT INTO Table(Column2) VALUES('#{b}')")
    end

    c.each do |c|
      puts c
      db.query("INSERT INTO Table(Column3) VALUES('#{c}')")
    end

    d.each do |d|
      puts d
      db.query("INSERT INTO Table(Column4) VALUES('#{d}')")
    end
end

Obviously this is just an example but the blocks don’t really differ much from this.

A few things.

First, yes, I need to have these four blocks with the .each because I am doing different things with them that I can’t just do all in one block.

Second, I know that each of these blocks is “right” because if I comment out all of them but one, it will write perform that one block correctly – writing to the database, etc. With all of the blocks uncommented like they should be, it only writes block ‘a’ to the database and the rest of the blocks don’t write anything, leaving the rows NULL.

The ‘puts’ commands are outputting perfectly fine as expected – each block executing after the one before, there is just an issue when it is trying to do the query() method.

Any ideas?

  • 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-30T06:03:46+00:00Added an answer on May 30, 2026 at 6:03 am

    I’ve never used Ruby before but perhaps you have an issue with disposing of the query object or properly initialising it again before using it for subsequent blocks?

    Try re-initialising the query object.

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

Sidebar

Related Questions

I'm working from an example piece of code that allocates a relatively large local
I'm working on an app that writes to excel. The following piece f code
I am working on a piece of code that copies a 'Person' object from
I'm working on a piece of code that I inherited and am trying to
I'm working a piece of code to turn phone numbers into links for mobile
I'm currently working in a piece of code where both logic and data access
I've been working on this small piece of code that seems trivial but still,
I am currently working with a piece of code that has been written by
I am working on a piece of code that I am wanting to spice
I am working with a piece of code that I find kinda strange. The

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.