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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:58:14+00:00 2026-06-12T19:58:14+00:00

I exported tables and queries from SQL. The ruby (1.9+) way to read csv

  • 0

I exported tables and queries from SQL.

The ruby (1.9+) way to read csv appears to be:

require 'csv'

CSV.foreach("exported_mysql_table.csv", {:headers=>true}) do |row|
    puts row
end

Which works great if your data is like this:

"name","email","potato"
"Bob","bob@bob.bob","omnomnom"
"Charlie","char@char.com","andcheese"
"Doug","diggyd@diglet.com","usemeltattack"

Works fine (The first line is a header, the attributes). However, if the data is like this:

"id","name","email","potato"
1,"Bob","bob@bob.bob","omnomnom"
2,"Charlie","char@char.com","andcheese"
4,"Doug","diggyd@diglet.com","usemeltattack"

Then we get the error:

.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/csv.rb:1894:in `block (2 levels) in shift': Missing or stray quote in line 2 (CSV::MalformedCSVError)

I think this is because the id is stored as a number, not a string, and thus has no quotes, and the csv parser expects ALL the entries to have quotes. Ideally I’d like to read “Bob” as a string and 1 as a number (and stuff it into a Hash of hashes)

(Have tried ‘FasterCSV’, that gem became ‘csv’ since ruby 1.9)

EDIT:

Was pointed out that the example worked fine (derp), was looking in the wrong place, it was an error with multi-line fields, question moved to Ruby CSV read multiline fields

  • 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-12T19:58:16+00:00Added an answer on June 12, 2026 at 7:58 pm

    Using the input you provided, I am unable to reproduce this.

    1.9.3p194 :001 > require 'csv'
     => true 
    1.9.3p194 :002 > CSV.foreach("test.txt", {:headers => true}) { |row| puts row }
    1,Bob,bob@bob.bob,omnomnom
    2,Charlie,char@char.com,andcheese
    4,Doug,diggyd@diglet.com,usemeltattack
     => nil
    

    The only difference I see between our environments is that you are using rbenv, and I am using RVM. I also verified this on another machine I have with ruby 1.9.3-p194. Does the input you provided exactly match what is in your csv?

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

Sidebar

Related Questions

I exported tables and queries from SQL, where some of the fields are multi-line.
I have exported many times tables from Hive to SQL Server. I never face
Using MySQL Administrator GUI tool I have exported some data tables retrieved from an
So I exported two tables in their entirety out of SQL (2005) into an
I've exported a table from a MySQL database, choosing the JSON export option. When
How to make exported .XLS file Editable Thid code makes .XLS File Read Only
I have a text file exported from a Foxpro (Dos-based) program, but this text
I have a strongly typed ADO.NET dataset containing several tables exported as an XML
I have an access db and am looking to export the tables to csv.
I would like to export a ad hoc Select query result sets from SQL

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.