Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

MySQL has a CSV storage engine.

I'm wondering if I could create such a table directly from an existing CSV file.

Made up command as example:

CREATE TABLE test (i INT NOT NULL, c CHAR(10) NOT NULL) ENGINE = CSV FROM FILE /file;

What I'm trying to achieve is an efficient way to load data that doesn't depend on LOAD DATA as that command opens up the possibility of arbitrary command execution.

share|improve this question
add comment

1 Answer

Here is something a MySQL Daredevil is more than willing to try.

I just did the following in MySQL 5.6.14 right on my Laptop

C:\Windows\system32>mysql -Dtest
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL://localhost/redwards/3306 test :: create table dt (a INT NOT NULL,b CHAR(10) NOT NULL) ENGINE=CSV;
Query OK, 0 rows affected (0.08 sec)

MySQL://localhost/redwards/3306 test :: insert into dt values (1,'rolando'),(2,'pamela'),(3,'dominique'),(4,'diamond');
Query OK, 4 rows affected (0.11 sec)
Records: 4  Duplicates: 0  Warnings: 0

MySQL://localhost/redwards/3306 information_schema :: flush tables;
Query OK, 0 rows affected (0.31 sec)

MySQL://localhost/redwards/3306 test :: exit
Bye

Now, going into the OS:

C:\Windows\system32>cd \MySQL_5.6.14\data\test

C:\MySQL_5.6.14\data\test>dir
 Volume in drive C has no label.
 Volume Serial Number is A06D-D0B3

 Directory of C:\MySQL_5.6.14\data\test

12/18/2013  04:18 PM    <DIR>          .
12/18/2013  04:18 PM    <DIR>          ..
10/24/2013  10:20 AM                65 db.opt
12/18/2013  04:20 PM                35 dt.CSM
12/18/2013  04:19 PM                49 dt.CSV
12/18/2013  04:18 PM             8,578 dt.frm
10/29/2013  06:06 PM                 0 exp1#P#part00.MYD
10/29/2013  06:06 PM             1,024 exp1#P#part00.MYI
10/29/2013  06:06 PM                 0 exp1#P#part01.MYD
10/29/2013  06:06 PM             1,024 exp1#P#part01.MYI
10/29/2013  06:06 PM                 0 exp1#P#part02.MYD
10/29/2013  06:06 PM             1,024 exp1#P#part02.MYI
10/29/2013  06:06 PM               336 exp1#P#part03.MYD
11/15/2013  05:44 PM             2,048 exp1#P#part03.MYI
10/29/2013  06:06 PM                 0 exp1#P#part99.MYD
10/29/2013  06:06 PM             1,024 exp1#P#part99.MYI
10/29/2013  06:06 PM             8,642 exp1.frm
10/29/2013  06:06 PM                60 exp1.par
10/24/2013  10:30 AM             8,682 menus.frm
10/24/2013  10:30 AM                68 menus.MYD
10/28/2013  03:00 PM             1,024 menus.MYI
10/24/2013  10:30 AM             8,584 pages.frm
10/24/2013  10:30 AM                80 pages.MYD
10/28/2013  03:00 PM             2,048 pages.MYI
              22 File(s)         44,395 bytes
               2 Dir(s)  1,869,894,590,464 bytes free

C:\MySQL_5.6.14\data\test>type dt.CSV
1,"rolando"
2,"pamela"
3,"dominique"
4,"diamond"

C:\MySQL_5.6.14\data\test>

As show in the above commands in mysql and in the Windows command line, the CSV file that mysql can handle has the following two characteristics:

  • fields delimited by commas
  • character fields enclosed in double quotes

Since you created the test table as CSV, here is what you should do:

  1. Make sure the existing CSV file has the two characteristics for mysql to work with it
  2. Go to the folder test in your datadir.
  3. Rename test.CSV to test1.CSV
  4. Copy your existing CSV file to test.CSV
  5. Login to mysql and run FLUSH TABLES;

Afterwards, the table shold be fully accessiable.

CAVEAT (Windows)

If Windows has the CSV table locked, run these commands

  1. Make sure the existing CSV file has the two characteristics for mysql to work with it
  2. Go to the folder test in your datadir.
  3. Run net stop mysql
  4. Rename test.CSV to test1.CSV
  5. Copy your existing CSV file to test.CSV
  6. Run net start mysql

Give it a Try !!!

share|improve this answer
    
That's awesome, though I wonder what the chances are this will be allowed in a production environment. It does indeed seem like something that requires a daredevil. ;) Might still be considered preferable to allowing LOAD DATA though. I'll leave the Q open for a while longer to see if anything else comes up. –  user50849 Dec 19 '13 at 12:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.