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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:09:51+00:00 2026-06-06T18:09:51+00:00

I am returning query results from MySQL and I need to append them to

  • 0

I am returning query results from MySQL and I need to append them to a csv file. I can retrieve the data and display it successfully but am having trouble getting it written to a csv. Here is my Ruby so far.

#! /usr/bin/ruby

require 'mysql'
require 'csv'

# variables
file_path = '/Users/pierce/tmp/'

# need mysql queries here / use mysql2 calls
test_db = Mysql.new("localhost", "root", "", "db")

series_1_results = test_db.query("SELECT 'Impressions' AS label_imp,FORMAT(sum(impressions),',') AS impressions FROM table ")

series_1_results.each_hash do |f|
  puts "#{f['label_imp']}"",\"""#{f['impressions']}""\""
end

The last statement puts the correct result: Impressions,”34,017″. When I remove it and include the CSV.open statement below I get the error that follows. Line 87 begins: csv <<

CSV.open("#{file_path}"'test_file.csv', "wb") do |csv|
    series_1_results.each_hash do |s1|
      csv << ["#{s1['label_imp']}","#{s1['impressions']}"]
    end
end

test_csv.rb:87:in `[]': can't convert String into Integer (TypeError)

I am running Ruby 1.9.2 and appreciate any advice you have.

  • 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-06T18:09:52+00:00Added an answer on June 6, 2026 at 6:09 pm

    With the mysql gem (mysql2 is different) you can only iterate through a mysql result set with each_hash once: after a row has been consumed mysql no longer returns it to you because it thinks you are done with it. Your first call to each_hash consumes all the rows, so when you iterate through it a second time there are no rows left and you get no output.

    If you remove your puts version of the output it should work

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

Sidebar

Related Questions

The query below is not returning any results. I have copied it from a
I've got a mysql query that is returning the required data, but is not
Why is my query not returning any results? I can run the query SELECT
i have a MySQL SELECT query which fetches data from 6 tables using Mysql
My MySQL table is not returning results with a MATCH (col) AGAINST ('') query.
I am trying to build complex mysql query but its returning wrong results... SELECT
select station_id,count(case_id) from emer_complaint group by station_id Above query returning the correct result. But,
This is meant to be a query returning ranked results greater than zero. I'm
I have a simple Nhibernate Linq query that is returning more results than expected:
I have a query returning the counts of some data, but I do NOT

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.