Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby-pg sanitize data before insert

Ruby newbie here. I'm trying to insert this string "Lady Arabella's Scandalo..." I'm using ruby-pg to do this. However I'm having erros because of the single quote, how can I sanitize this string and remove all html tags? Is there a built in function for this?

like image 825
Diffy Avatar asked May 28 '12 03:05

Diffy


1 Answers

You can use escape_string to properly escape your single quotes:

db = PG.connect(...)
db.exec("insert into t (...) values ('#{db.escape_string(str)}', ...)")

or use prepare and exec_prepared to work with a prepared statement instead:

db.prepare('ins', 'insert into t (...) values ($1, ...)')
db.exec_prepared('ins', [str, ...])
like image 163
mu is too short Avatar answered Oct 02 '22 02:10

mu is too short