\ '(backslash, single quote) in a Ruby string

I am using Ruby 1.8.7, trying to generate a string with \ characters to create a script to work in MySQL. The result should look like this:

INSERT INTO table (name, description) values ('Joana d\'Arc', '') 

But I can't get just one backslash in a ruby ​​string. Using the following code:

 string = "INSERT INTO table (name, description) values ('Joana d\\'Arc', '')" 

I got the following line:

 INSERT INTO table (name, description) values ('Joana d\\'Arc', '') 

And with the help of:

 string = "INSERT INTO table (name, description) values ('Joana d\'Arc', '')" 

I got this line:

 INSERT INTO table (name, description) values ('Joana d'Arc', '') 
+4
source share
4 answers

The reason is that it shows what is actually in the string. The bare console displays the string that has been escaped. Try this on the console:

 "Joana d\\'Arc".size 

You will go back 12. If both backslashes were there, you should get 13.

Hauleth answer should work.

+3
source

I recommend avoiding writing the original SQL, and at that age I would use ORM, even for simple database use. I highly recommend using the Sequel gem. Borrowing from in Sequel docs:

 sequel sqlite://temp Your database is stored in DB... 

Using SQLite, which launched the ORM sequel interactively and created an SQLite database called "temp".

 ruby-1.9.2-p290 :001 > require 'logger' => true ruby-1.9.2-p290 :002 > DB.loggers << Logger.new(STDOUT) => [#<Logger:0x0000010160bc40 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x0000010160bc18 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x0000010160bbc8 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x0000010160bba0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x0000010160bb50>>>>] 

This allowed us to keep a journal so we can see what Sequel will do when it talks to the database.

 ruby-1.9.2-p290 :003 > items = DB[:items] # Create a dataset => #<Sequel::SQLite::Dataset: "SELECT * FROM `items`"> ruby-1.9.2-p290 :004 > DB.tables I, [2011-11-25T10:17:13.056311 #10130] INFO -- : (0.000501s) SELECT * FROM `sqlite_master` WHERE (type = 'table' AND NOT name = 'sqlite_sequence') => [] 

Doh! I forgot to create a table ...

 ruby-1.9.2-p290 :005 > DB.create_table :items do ruby-1.9.2-p290 :006 > primary_key :id ruby-1.9.2-p290 :007?> String :name ruby-1.9.2-p290 :008?> Float :price ruby-1.9.2-p290 :009?> end I, [2011-11-25T10:17:20.985851 #10130] INFO -- : (0.002372s) CREATE TABLE `items` (`id` integer PRIMARY KEY AUTOINCREMENT, `name` varchar(255), `price` double precision) => nil 

Created table.

 ruby-1.9.2-p290 :010 > items = DB[:items] # Create a dataset => #<Sequel::SQLite::Dataset: "SELECT * FROM `items`"> 

This created a dataset, which is just a convenient way to talk to a table.

And, what is the gain:

 ruby-1.9.2-p290 :011 > items.insert(:name => "Joan d'Arc") I, [2011-11-25T10:17:45.186945 #10130] INFO -- : (0.001981s) INSERT INTO `items` (`name`) VALUES ('Joan d''Arc') => 1 

ORM automatically deletes characters for you. Your work is greatly simplified.

ORM is DBM, so they know when to run for a particular database. Your code does not change. It is trivial to switch from SQLite to MySQL, Postgres, or even non-SQL databases.

+6
source

Record it using the notation %q{}

 string = %q[INSERT INTO table (name, description) values ('Joanna d\\'Arc', '')] 
+1
source

You can try the following:

 string="INSERT INTO table (name, description) values ('Joana d"+"\\"+"'Arc','')" 

I do not know whether to use 1.8.6, but if you can, upgrade to 1.9.2. It is much faster and more stable, as well as many other functions.

0
source

Source: https://habr.com/ru/post/1383095/


All Articles