1

I have a string:

CREATE TABLE IF NOT EXISTS  `coupons` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` varchar(255) NOT NULL,
  `off` int(11) NOT NULL,
  `order` int(11) NOT NULL,
  `value` tinyint(1) DEFAULT NULL,
  `status` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

CREATE TABLE `currency` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` varchar(10) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `prefix` varchar(12) DEFAULT NULL,
  `suffix` varchar(12) DEFAULT NULL,
  `format` char(1) DEFAULT NULL,
  `decimals` char(1) DEFAULT NULL,
  `exchange` float(15,6) DEFAULT NULL,
  `round` int(5) NOT NULL,
  `published` tinyint(1) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

And would like to replace: "CREATE TABLE" to "CREATE TABLE IF NOT EXISTS". How can I do this?

Thanks!

XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60
  • 2
    As it look like a one time action, 'find and replace' tool in any text editor should do :) – Marcin Oct 28 '15 at 01:02
  • Seriously? There are so many PHP [replace](http://stackoverflow.com/questions/1072476/php-replace-but-alternate-replace-string) [strings](http://stackoverflow.com/questions/2590044/php-replace-string-help) [questions](http://stackoverflow.com/questions/3392508/replace-string-only-once-with-php-preg-replace) [already](http://stackoverflow.com/questions/3441000/php-replace-string). With a little bit of searching it should be obvious what to do. This question has no added value to the community. – cfi Oct 28 '15 at 07:37

1 Answers1

3

In PHP (if you really need to do it programmatically) you'd do something like this (assuming $string is your variable containing both SQL commands):

$string = preg_replace('/CREATE\sTABLE\s+`/i', 'CREATE TABLE IF NOT EXISTS `', $string);

Works: https://gist.github.com/jpaljasma/8355c57cc5608c4c4ff8

jpaljasma
  • 1,612
  • 16
  • 21