Tuesday, March 29, 2016

mysqldump, avoid escape double quotes

Leave a Comment

When I have a field with double quotes, mysqldump put the escape character before. For example:

'My "test"' -> mysqldump generates a file with that field like 'My \"test\"'

The problem is that I'm using that file to import some data into a sqlite database, and SQLite doesn't remove the escape character. So I don't need that mysqldump writes escape characters, can I do that?

3 Answers

Answers 1

I am unsure what language you are using. But the concept would be the same in any programming language.

First declare a variable equal to your sqldump.

var yourVariable = sqldump; 

Then do a string.replace("\\", ""). Then use that 'cleaned' version to import your data with.

Answers 2

Which SQL mode do you use? Look at the mysqldump manual: http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html

--quote-names, -Q

Quote identifiers (such as database, table, and column names) within “`” characters. If the ANSI_QUOTES SQL mode is enabled, identifiers are quoted within “"” characters. This option is enabled by default. It can be disabled with --skip-quote-names, but this option should be given after any option such as --compatible that may enable --quote-names.

--quote-names is enabled by default.

ANSI_QUOTES

Treat “"” as an identifier quote character (like the “`” quote character) and not as a string quote character. You can still use “`” to quote identifiers with this mode enabled. With ANSI_QUOTES enabled, you cannot use double quotation marks to quote literal strings, because it is interpreted as an identifier.

Answers 3

From a google search I found this which is reworking the output of mysqldump so sqlite can use it. Not tested and 5 years old but I think:

  • you may find useful information
  • it may mean other people didn't find a more elegant solution

EDIT:

As per the comments, to answer your question it looks like you don't have a choice but replace \" into ".

Here is a repo with a script which translates mysqldump files into something sqlite can digest. In particular, for your question, you can find this line:

gsub( /\\"/, "\"" ) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment