I have a “”“database”"" of 11513 shortwave transmitters with their
frequency, on-air time and a lot of other informations.
These database is in a “flat ascii file”: Each broadcaster gets one
line of 294 charcters. Each “column” is only sperated from the others
by known offsets.
My little ruby program reads line by line (broadcaster by
broadcaster) from this file and excerpts the wanted informations by
expressions like “frequncy = line[22…27]”.
Then an sql-statement is formed and sent to mysqld, which inserts the
data into the database.
So far so nice…
BUT!
There is one filed called “comment” in the database, where ANYTHING
printable can be exspected.
One line says has a comment like
International service "A".
At this point, mysqld jumps from the rail and says “STOP! SYNTAX
VIOLATED”.
Generally: how can I process input to escape “special” characters
before I send them to mysqld?
(In perl there is a command called “quotemeta”, which does the
thing…I found nothing equivalent in the docs for ruby).
Kind regards and thank you very much for your help in advance.
Meino
These database is in a “flat ascii file”: Each broadcaster gets one
line of 294 charcters. Each “column” is only sperated from the others
by known offsets.
[…]
Generally: how can I process input to escape “special” characters
Regexp.escape(aString)
before I send them to mysqld?
(In perl there is a command called “quotemeta”, which does the
Regexp.escape is also known as: Regexp.quote(aString)
Hugh
···
On Fri, 29 Aug 2003, Meino Christian Cramer wrote:
These database is in a “flat ascii file”: Each broadcaster gets one
line of 294 charcters. Each “column” is only sperated from the others
by known offsets.
[…]
Generally: how can I process input to escape “special” characters
Regexp.escape(aString)
before I send them to mysqld?
(In perl there is a command called “quotemeta”, which does the
Regexp.escape is also known as: Regexp.quote(aString)
Hugh
This is (nearly) exactly for what I have searched for…
Unfortunately the “'” is not escaped and this character is that,
what make my headache…
I have a broadcaster called “N’Djamena”.
The “'” in its name confuses the string sent to mySQL…
I tried (with irb) the following. But I cannot figure out, what
game ruby is playing with me…
mysql-ruby contains a class method: Mysql.escape_string (alias Mysql.quote)
which does exactly what you want.
Have you checked the documentation for ruby-mysql to see if it has such a
function? If not, maybe you should change.
I FOUND IT !!! OH HAPPY DAY !!! ;)))
Thank you for directly pointing me to things which I already have
:O)))
I was too blind…
But now another probelm hits me:
The data for my (hopefully soon existing) shortwave broadcaster
database is read line by line (broadcaster by broadcaster) from a
“pure text” file.
On position 279 there is a single character, which reflects the
status of the broadcaster. It one character of [A-Z].
I did the following:
status = line[279]
I got
72
(“H” is the 72scnd character of the ASCII table.)
Oh, I thought…better to do a
status = status.to_s()
Now I got a
"72"
Damn! In the input file there is literally a “H”. How can I convince
Ruby to read a “H” and no “72” or 72?
Sometimes I think, the “principle of leas surprise” isn’t working
everywhere to its completeness…
Kind regards,
Meino
···
From: Brian Candler B.Candler@pobox.com
Subject: Re: Quoted string problem
Date: Sat, 30 Aug 2003 18:26:15 +0900
By the way: I tried DBI but it seems not 1.8.0-ready ? I got a lot
of errors…
It works just fine for me. I get a couple of warnings only, no errors. I use
DBI-0.0.20 with mysql, OCI8, sqlite
I, too, am running Ruby 1.8, DBI 0.0.20 (but with Postgresql) without
problems.
mysql-ruby contains a class method: Mysql.escape_string (alias Mysql.quote)
which does exactly what you want.
Meino mentions later that he found a quote method. I would just like to
remind folks that if you are using DBI, the quote method is part of the
driver and can be accessed via …
The DBI version /should/ take into account different quoting
requirements of the individual drivers (if it doesn’t, that’s a bug!).
Explicit quoting isn’t needed if you are using the “?” parameters (as
you should!).