PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > How do I debug mysql syntax problems in ruby code?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How do I debug mysql syntax problems in ruby code?

Réponse
 
LinkBack Outils de la discussion
Vieux 08/06/2008, 22h21   #1
Michael Litchard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How do I debug mysql syntax problems in ruby code?

[Note: parts of this message were removed to make it a legal post.]

So, I have a mysql syntax problem in my ruby code. Being new to both, I'm
not sure how to proceed.
Here's the code.

!/usr/pkg/bin/ruby18 -w
# simple.rb - simple MySQL script using Ruby DBI module

require "dbi"
require "generator"

begin
lines= Array.new
dbh = DBI.connect("DBI:mysql:test:localhost","root","")
dbh.do("DROP TABLE IF EXISTS map")
dbh.do("CREATE TABLE map (
zip INT UNSIGNED,
st_abv CHAR(2),
latitude FLOAT,
longitude FLOAT, #experiment
city CHAR(20),
state CHAR(20),
PRIMARY KEY (zip))")
sth = dbh.prepare("INSERT INTO map (zip,st_abv,lat,long,city,state)
VALUES(?, ?, ?, ?, ?, ?)")
File.open("new.csv", "r") do |f|
f.each_line do |line|
lines.push(line.gsub(/\"/, '\''))
end
#lines.each {|line| print line }
end
lines.each do |line|
zip,st_abv,lat,long,city,state = line.chomp.split(",")
print zip,st_abv,lat,long,city,state
print "\n"
sth.execute(zip, st_abv, lat, long, city, state)
end
sth.finish
rescue DBI:atabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
dbh.disconnect if dbh
end


Here's the text file I'm using

"00968", "PR", " 18.408479", " -66.10250", "Guaynabo", "Puerto Rico"
"00969", "PR", " 18.366981", " -66.10889", "Guaynabo", "Puerto Rico"
"00971", "PR", " 18.329688", " -66.11876", "Guaynabo", "Puerto Rico"
"00976", "PR", " 18.346767", " -66.00561", "Trujillo Alto", "Puerto Rico"
"00979", "PR", " 18.431885", " -66.01270", "Carolina", "Puerto Rico"
"00982", "PR", " 18.409345", " -65.99313", "Carolina", "Puerto Rico"
"00983", "PR", " 18.414408", " -65.97582", "Carolina", "Puerto Rico"
"00985", "PR", " 18.374896", " -65.94691", "Carolina", "Puerto Rico"
"00987", "PR", " 18.372228", " -65.96275", "Carolina", "Puerto Rico"
"009HH", "PR", " 18.435287", " -66.06653", "", "Puerto Rico"

and here's the error message I got

$ ./test_slurp.rb
'00968' 'PR' ' 18.408479' ' -66.10250' 'Guaynabo' 'Puerto Rico'
An error occurred
Error code: 1064
Error message: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'long,city,state)
VALUES('\'00968\'', ' \'PR\'', ' \' 18.408' at line 1


thanks for your gang.

  Réponse avec citation
Vieux 08/06/2008, 23h28   #2
Michael Litchard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do I debug mysql syntax problems in ruby code?

[Note: parts of this message were removed to make it a legal post.]

I figured this out. I was using a reserved word.

On Sun, Jun 8, 2008 at 2:21 PM, Michael Litchard <michael@schmong.org>
wrote:

> So, I have a mysql syntax problem in my ruby code. Being new to both, I'm
> not sure how to proceed.
> Here's the code.
>
> !/usr/pkg/bin/ruby18 -w
> # simple.rb - simple MySQL script using Ruby DBI module
>
> require "dbi"
> require "generator"
>
> begin
> lines= Array.new
> dbh = DBI.connect("DBI:mysql:test:localhost","root","")
> dbh.do("DROP TABLE IF EXISTS map")
> dbh.do("CREATE TABLE map (
> zip INT UNSIGNED,
> st_abv CHAR(2),
> latitude FLOAT,
> longitude FLOAT, #experiment
> city CHAR(20),
> state CHAR(20),
> PRIMARY KEY (zip))")
> sth = dbh.prepare("INSERT INTO map (zip,st_abv,lat,long,city,state)
> VALUES(?, ?, ?, ?, ?, ?)")
> File.open("new.csv", "r") do |f|
> f.each_line do |line|
> lines.push(line.gsub(/\"/, '\''))
> end
> #lines.each {|line| print line }
> end
> lines.each do |line|
> zip,st_abv,lat,long,city,state = line.chomp.split(",")
> print zip,st_abv,lat,long,city,state
> print "\n"
> sth.execute(zip, st_abv, lat, long, city, state)
> end
> sth.finish
> rescue DBI:atabaseError => e
> puts "An error occurred"
> puts "Error code: #{e.err}"
> puts "Error message: #{e.errstr}"
> ensure
> dbh.disconnect if dbh
> end
>
>
> Here's the text file I'm using
>
> "00968", "PR", " 18.408479", " -66.10250", "Guaynabo", "Puerto Rico"
> "00969", "PR", " 18.366981", " -66.10889", "Guaynabo", "Puerto Rico"
> "00971", "PR", " 18.329688", " -66.11876", "Guaynabo", "Puerto Rico"
> "00976", "PR", " 18.346767", " -66.00561", "Trujillo Alto", "Puerto Rico"
> "00979", "PR", " 18.431885", " -66.01270", "Carolina", "Puerto Rico"
> "00982", "PR", " 18.409345", " -65.99313", "Carolina", "Puerto Rico"
> "00983", "PR", " 18.414408", " -65.97582", "Carolina", "Puerto Rico"
> "00985", "PR", " 18.374896", " -65.94691", "Carolina", "Puerto Rico"
> "00987", "PR", " 18.372228", " -65.96275", "Carolina", "Puerto Rico"
> "009HH", "PR", " 18.435287", " -66.06653", "", "Puerto Rico"
>
> and here's the error message I got
>
> $ ./test_slurp.rb
> '00968' 'PR' ' 18.408479' ' -66.10250' 'Guaynabo' 'Puerto Rico'
> An error occurred
> Error code: 1064
> Error message: You have an error in your SQL syntax; check the manual that
> corresponds to your MySQL server version for the right syntax to use near
> 'long,city,state)
> VALUES('\'00968\'', ' \'PR\'', ' \' 18.408' at line 1
>
>
> thanks for your gang.
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h37.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10708 seconds with 10 queries