Search code examples
mysqlload-data-infile

mysql import script


Is it possible to import csv data into mysql and automatically create the column names, as in can I create just the table, or must I create the table names as well?

Is it possible to check for duplicate entries upon importing? I have an identifier field, but dont know how to make it so it will not be imported twice.

How would you import a jpeg file on a website into a field? Assume the website has been stored locally, and has the same filename as an identifer with ".jpeg" added on to the end.


Solution

  • As tante said you'll have to handle the table creation yourself, but as far as importing csv is concerned you should have a look at LOAD DATA INFILE

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]
    

    Duplicate entries won't be a problem if you have set primary keys on your table.

    The jpeg question seems like a completely different issue (unless the data is in your csv)