{"id":4364,"date":"2011-11-15T08:11:37","date_gmt":"2011-11-15T06:11:37","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=4364"},"modified":"2011-11-15T08:16:42","modified_gmt":"2011-11-15T06:16:42","slug":"quoting-text-javascriptpython-style","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/quoting-text-javascriptpython-style","title":{"rendered":"Quoting text JavaScript\/Python style"},"content":{"rendered":"<p>Unless your MySQL is configured to use <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/server-sql-mode.html#sqlmode_ansi_quotes\"><strong>ANSI_QUOTES<\/strong><\/a> in <strong>sql_mode<\/strong>, you are able to quote your text in one of two forms: using single quotes or double quotes:<\/p>\n<blockquote>\n<pre>UPDATE world.Country SET HeadOfState = <strong>'Willy Wonka'<\/strong> WHERE Code=<strong>'USA'<\/strong>\r\nUPDATE world.Country SET HeadOfState = <strong>\"Willy Wonka\"<\/strong> WHERE Code=<strong>\"USA\"<\/strong><\/pre>\n<\/blockquote>\n<p>This makes for JavaScript- or Python-style quoting: you quote by your needs. Say you have a text which includes single quotes:<\/p>\n<blockquote><p><em>It is what you read when you don&#8217;t have to that determines what you will be when you can&#8217;t help it.<\/em> &#8211; Oscar Wilde<\/p><\/blockquote>\n<p>You wish to insert this text to some tables. You could go through the trouble of escaping it:<\/p>\n<blockquote>\n<pre>INSERT INTO quotes (quote, author) VALUES (\r\n\u00a0 'It is what you read when you don\\'t have to that determines what you will be when you can\\'t help it.', 'Oscar Wilde');<\/pre>\n<\/blockquote>\n<p>or you could just wrap it in double quotes:<\/p>\n<blockquote>\n<pre>INSERT INTO quotes (quote, author) VALUES (\r\n\u00a0 \"It is what you read when you don't have to that determines what you will be when you can't help it.\", 'Oscar Wilde');<\/pre>\n<\/blockquote>\n<p>I find this useful when using SQL to generate queries. Take, for example, <a href=\"http:\/\/code.openark.org\/blog\/mysql\/mysql-eval\">eval() for MySQL<\/a>: the statement:<!--more--><\/p>\n<blockquote>\n<pre>CALL eval('select concat(\\'KILL \\',id) from information_schema.processlist where user=\\'webuser\\'');<\/pre>\n<\/blockquote>\n<p>is just so more easily written this way:<\/p>\n<blockquote>\n<pre>CALL eval(\"select concat('KILL ',id) from information_schema.processlist where user='webuser'\");<\/pre>\n<\/blockquote>\n<p>I don&#8217;t suggest one should use this method throughout her application code. Application code works great with auto-escaping string literals. But for the handy DBA or developer, who needs to work some quick queries by hand, this makes for an easier syntax to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unless your MySQL is configured to use ANSI_QUOTES in sql_mode, you are able to quote your text in one of two forms: using single quotes or double quotes: UPDATE world.Country SET HeadOfState = &#8216;Willy Wonka&#8217; WHERE Code=&#8217;USA&#8217; UPDATE world.Country SET HeadOfState = &#8220;Willy Wonka&#8221; WHERE Code=&#8221;USA&#8221; This makes for JavaScript- or Python-style quoting: you quote [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[5],"tags":[21,20],"class_list":["post-4364","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-sql","tag-syntax"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-18o","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=4364"}],"version-history":[{"count":8,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4364\/revisions"}],"predecessor-version":[{"id":4372,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/4364\/revisions\/4372"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=4364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=4364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=4364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}