String replace ' for mySQL insert

  • Thread starter Roland Klein overmeer
  • Start date
R

Roland Klein overmeer

Hello All,

I'm a newbie, just started programming Ruby about 1 month ago. Have lots
of experience with vb so the programming concepts are clear to me. I'm
building a script that is using the sample stream of the Twitter API and
insert it into MySQL for analysis. The problem is, that some of the
tweets have a ' in them. I want to replace them with '' so insert into
SQL is not a problem. I have written some piece of code trying to get
this to work, but the output replaces every letter with ''. This is the
code:

if line.include?"'"
result=""
teller=line.length-1
for i in 1..teller do
if line="'"
result=result + "''"
else
result=result + line
end
end
line=result
end

What am I doing wrong?

Thanks,
Roland
 
B

Brian Candler

Roland Klein overmeer wrote in post #977069:
I'm a newbie, just started programming Ruby about 1 month ago. Have lots
of experience with vb so the programming concepts are clear to me. I'm
building a script that is using the sample stream of the Twitter API and
insert it into MySQL for analysis. The problem is, that some of the
tweets have a ' in them. I want to replace them with '' so insert into
SQL is not a problem. I have written some piece of code trying to get
this to work, but the output replaces every letter with ''. This is the
code:

You're probably going about this the wrong way - your database layer
should provide a quoting mechanism for you. For example, if you're using
the low-level mysql-ruby then use Mysql.quote(str); if you're using
something like ActiveRecord then that handles quoting for you.

But to answer your direct question:

line.gsub!("'","''")

would achieve what you're asking for.
 
I

Ian M. Asaff

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

in VB, '=' can means both assignment and equality, so the following does
what you expect:

in ruby, you want '==' for equality (and '=' for assignment), so the above
line should be




On Mon, Jan 24, 2011 at 4:17 AM, Roland Klein overmeer <
Hello All,

I'm a newbie, just started programming Ruby about 1 month ago. Have lots
of experience with vb so the programming concepts are clear to me. I'm
building a script that is using the sample stream of the Twitter API and
insert it into MySQL for analysis. The problem is, that some of the
tweets have a ' in them. I want to replace them with '' so insert into
SQL is not a problem. I have written some piece of code trying to get
this to work, but the output replaces every letter with ''. This is the
code:

if line.include?"'"
result=""
teller=line.length-1
for i in 1..teller do
if line="'"
result=result + "''"
else
result=result + line
end
end
line=result
end

What am I doing wrong?

Thanks,
Roland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top