Click here to Skip to main content
15,890,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need a Regular Expression to split all SQL query in a file..

like that file I need to split all queries from it.. to an array..

CREATE TABLE IF NOT EXISTS `collections` (
  `collectionid` int(3) unsigned NOT NULL auto_increment,
  `collectiongroupid` int(2) NOT NULL,
  `name` text NOT NULL,
  `productsCount` int(6) NOT NULL,
  PRIMARY KEY  (`collectionid`)
);

INSERT INTO `collections` (`collectionid`, `collectiongroupid`, `name`, `productsCount`) VALUES
(1, 1, 'Chairs', 9),
(2, 1, 'Beds', 5),
(65, 5, 'Desks', 1),
(67, 1, 'Longers', 3),
(72, 1, 'Sofas', 5),
(73, 5, 'Tables', 2);

CREATE TABLE IF NOT EXISTS `mailing_queue` (
  `mailqueueid` int(10) unsigned NOT NULL auto_increment,
  `mailid` int(10) NOT NULL,
  `mails` text NOT NULL,
  `userid` int(3) NOT NULL,
  `ipaddress` varchar(25) NOT NULL,
  `dateline` datetime NOT NULL,
  PRIMARY KEY  (`mailqueueid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=35 ;


INSERT INTO `mailing_queue` (`mailqueueid`, `mailid`, `mails`, `userid`, `ipaddress`, `dateline`) VALUES
(1, 10, 'a:2:{i:0;s:18:"email@server.com";i:1;s:20:"emailfrom@server.com";}', 1, '127.0.0.1', '2010-11-24 08:14:07'),
(2, 10, 'a:2:{i:0;s:21:"email@server.com";i:1;s:18:"email@server.com";}', 1, '127.0.0.1', '2010-11-24 08:14:07'),
(3, 10, 'a:2:{i:0;s:13:"email@server.com";i:1;s:28:"email@server.com";}', 1, '127.0.0.1', '2010-11-24 08:14:07');


can anybody split queries in an arraylist using C#

string DataFile = @"Queries above";
string[] Queries = DataFile.Split(';'); // that's wrong Coz the forth query including ';' ..??


and tnx :)
Posted

1 solution

Bit crude, but take a look at the following code:

private void splitIt(string theQuery)
{
   List<string> theList = new List<string>();
   string[] theS = theQuery.Split(' ');
   StringBuilder s = new StringBuilder();
   foreach (var item in theS)
   {
      if ((item == "CREATE") || (item == "INSERT"))
      {
         if (s.ToString().Count() > 0)
         {
            theList.Add(s.ToString());
            s.Remove(0, s.Length);
            s.Append(item + " ");
         }
         else
            s.Append(item + " ");
      }
      else
      {
         s.Append(item + " ");
      }
   }
   theList.Add(s.ToString());
   string[] arr = theList.ToArray();
}


Pass your Datafile as theQuery parameter.
 
Share this answer
 
Comments
abdugo 28-Jun-11 11:46am    
sorry, but it's return always one query and don't remove any other words.. :(

i want to split them in regex only INSER QUERIES ..

INSERT INTO [(](.*)[)] VALUES [(](.*)[);]

but this don't work.. :S

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900