Search code examples
mysqlsqlcsvformat-conversion

Converting SQL file To CSV


I have dumped a sql table for cities, regions & countries.

I have the .sql files in my local machine. I need to convert them to CSV format.

So I have a 2 part question.

1): What is the best way to do this? Is any tools I can use? (I have a Mac)

2):

I found this site. So I tried with this code:

CREATE TABLE IF NOT EXISTS `countries` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `code` varchar(10) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=231 DEFAULT CHARSET=latin1;

-- Dumping data for table dddblog.countries: ~218 rows (approximately)
/*!40000 ALTER TABLE `countries` DISABLE KEYS */;
INSERT INTO `countries` (`id`, `name`, `code`) VALUES
    (1, 'Andorra', 'ad'),
    (2, 'United Arab Emirates', 'ae'),
    (3, 'Afghanistan', 'af'),
    (4, 'Antigua and Barbuda', 'ag'),
    (5, 'Anguilla', 'ai'),
    (6, 'Albania', 'al'),
    (7, 'Armenia', 'am'),
    (8, 'Netherlands Antilles', 'an'),
    (9, 'Angola', 'ao'),
    // Other countries

/*!40000 ALTER TABLE `countries` ENABLE KEYS */;

And when I click on Convert, I get error: Missing SELECT STATEMENT.

Im not familiar with SQL and any help is appreciated!


Solution

  • The message Missing SELECT STATEMENT says it all: You have to add a SELECT statement.

    Try it with this code - worked for me:

    CREATE TABLE IF NOT EXISTS `countries` (
      `id` smallint(5) NOT NULL ,
      `name` varchar(255) NOT NULL,
      `code` varchar(10) NOT NULL
    );
    
    
    INSERT INTO `countries` (`id`, `name`, `code`) VALUES
        (1, 'Andorra', 'ad'),
        (2, 'United Arab Emirates', 'ae'),
        (3, 'Afghanistan', 'af'),
        (4, 'Antigua and Barbuda', 'ag'),
        (5, 'Anguilla', 'ai'),
        (6, 'Albania', 'al'),
        (7, 'Armenia', 'am'),
        (8, 'Netherlands Antilles', 'an'),
        (9, 'Angola', 'ao')
        ;
    
    SELECT     `id`, `name`, `code` FROM `countries`