I have a file with SQL queries similar to this:
INSERT INTO Bank (bankCode, swiftCode, bankName, country, countryCode, locationCode,
address, city)
VALUES ('MIDL', 'MIDLGB2102N', 'Hsbc Bank Plc', 'United Kingdom', 'GB',
'21', '', 'Bishop's Stortford');
I have to replace all single quotes inside a string literal in order to be able to execute the query.
Can someone provide a regular expression in order to achieve that?
For example the above query should be:
INSERT INTO Bank (bankCode, swiftCode, bankName, country, countryCode,
locationCode,
address, city)
VALUES ('MIDL', 'MIDLGB2102N', 'Hsbc Bank Plc', 'United Kingdom', 'GB',
'21', '', 'Bishop''s Stortford');
Replace 'Bishop's Stortford' with 'Bishop''s Stortford' . Replace ' with ''
You have PDO::quote function in PHP for this purpose. Check this link: http://php.net/manual/en/pdo.quote.php
If you want a pattrn for regex replace: /(\\(|\\s)'([\\w ']*)'(,|\\))/
Or in PHP: preg_replace("/(\\(|\\s)'([\\w ']*)'(,|\\))/", '$1"$2"$3', $line);
The easiest approach would likely be something akin to:
/[^,(]\\s*'\\s*[^,)]/
This would match all ' characters which are not preceded by or following a comma or a paren (only other valid MySQL characters). I included a search for spaces because those are also possible valid characters.
That would certainly match everything. I'm not terribly familiar with how MySQL Workbench parses replaces in regular expressions, so I cannot be much assistance there.
To import via MySQL Workbench do the following:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.