Note that there are some explanatory texts on larger screens.

plurals
  1. POrails 3 + mysql - dealing with invalid characters when inserting into mysql
    primarykey
    data
    text
    <p>I am using rails 3 with mysql and am facing an issue with invalid characters when adding some text to my mysql db. </p> <p>Following is the mysql command causing issues (from my server log):</p> <pre><code>SQL (2.1ms) describe `shared_link_posts` AREL (0.4ms) INSERT INTO `posts` (`created_at`, `description`, `link`, `host_name`, `updated_at`, `content`) VALUES ('2011-07-05 00:54:51', NULL, 'http://foo.bar', 'foobar.com', '2011-07-05 00:54:51', 'foobar? foobar') </code></pre> <p>The issue is with the last value being added, namely <code>'foobar? foobar'</code>. The value added to the database is just <code>"foobar"</code>. Everything after and including the <code>"?"</code> is truncated, not added to the db.</p> <p>The <code>"?"</code> is an invalid character. It is \240 which in ascii corresponds to a hyphen.</p> <p>I was in the impression that rails uses parametrized sql queries which takes care of all these invalid characters when adding content to the mysql db. Does it not?</p> <p>How else can I solve this issue? I will be dealing with a lot of text that may or may not have invalid characters.</p> <p>Thanks!</p> <p>EDIT: The code I use to save to the db is simply just assigning a field this content. So it's basically:</p> <pre><code>post.content = body_content post.save! </code></pre> <p>body_content is <code>"foobar? foobar"</code></p> <p>EDIT:</p> <p>mysql> show variables like "character_set_database";</p> <pre><code>+------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | character_set_database | utf8 | +------------------------+-------+ 1 row in set (0.11 sec) mysql&gt; show variables like "collation_database"; +--------------------+-----------------+ | Variable_name | Value | +--------------------+-----------------+ | collation_database | utf8_unicode_ci | +--------------------+-----------------+ 1 row in set (0.02 sec) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload