Generate insert SQL statements from a CSV file

后端 未结 12 2115
轮回少年
轮回少年 2020-12-04 16:07

I need to import a csv file into Firebird and I\'ve spent a couple of hours trying out some tools and none fit my needs.

The main problem is that al

12条回答
  •  我在风中等你
    2020-12-04 17:11

    I'd do this with awk.

    For example, if you had this information in a CSV file:

    Bob,New York
    Jane,San Francisco
    Steven,Boston
    Marie,Los Angeles
    

    The following command will give you what you want, run in the same directory as your CSV file (named name-city.csv in this example).

    $ awk -F, '{ print "INSERT INTO PERSON (ID, NAME, CITY_ID) VALUES ((SELECT NEW_GUID FROM CREATE_GUID), '\''"$1"'\'', (SELECT CITY_ID FROM CITY WHERE NAME = '\''"$2"'\''))" }' name-city.csv
    

    Type awk --help for more information.

提交回复
热议问题