Generate insert SQL statements from a CSV file

后端 未结 12 2099
轮回少年
轮回少年 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:04

    use the csv-file as an external table. Then you can use SQL to copy the data from the external table to your destination table - with all the possibilities of SQL. See http://www.firebirdsql.org/index.php?op=useful&id=netzka

    0 讨论(0)
  • 2020-12-04 17:05

    You could import the CSV file into a table as is, then write an SQL query that does all the required transformations on the imported table and inserts the result into the target table.

    So something like:

    <(load the CSV file into temp_table - n, city_name)>

    insert into target_table

    select t.n, c.city_id as city

    from temp_table t, cities c

    where t.city_name = c.city_name

    Nice tip about using Excel, but I also suggest getting comfortable with a scripting language like Python, because for some task it's easier to just write a quick python script to do the job than trying to find the function you need in Excel or a pre-made tool that does the job.

    0 讨论(0)
  • 2020-12-04 17:05

    option 1: 1- have you tried IBExert? IBExpert \ Tools \ Import Data (Trial or Customer Version).

    option 2: 2- upload your csv file to a temporary table with F_BLOBLOAD. 3- create a stored procedure, which used 3 functions (f_stringlength, f_strcopy, f_MID) you cross all your string, pulling your fields to build your INSERT INTO.

    links: 2: http://freeadhocudf.org/documentation_english/dok_eng_file.html 3: http://freeadhocudf.org/documentation_english/dok_eng_string.html

    0 讨论(0)
  • 2020-12-04 17:08

    Well, if it's a CSV, and it this is a one time process, open up the file in Excel, and then write formulas to populate your data in any way you desire, and then write a simple Concat formula to construct your SQL, and then copy that formula for every row. You will get a large number of SQL statements which you can execute anywhere you want.

    0 讨论(0)
  • 2020-12-04 17:08

    Fabio,

    I've done what Vaibhav has done many times, and it's a good "quick and dirty" way to get data into a database.

    If you need to do this a few times, or on some type of schedule, then a more reliable way is to load the CSV data "as-is" into a work table (i.e customer_dataload) and then use standard SQL statements to populate the missing fields.

    (I don't know Firebird syntax - but something like...)

    UPDATE person
    SET id = (SELECT newguid() FROM createguid)
    
    UPDATE person
    SET cityid = (SELECT cityid FROM cities WHERE person.cityname = cities.cityname)
    

    etc.

    Usually, it's much faster (and more reliable) to get the data INTO the database and then fix the data than to try to fix the data during the upload. You also get the benefit of transactions to allow you to ROLLBACK if it does not work!!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题