Insert SQL statements via command line without reopening connection to remote database

后端 未结 2 1371
轮回少年
轮回少年 2021-02-05 18:16

I have a large amount of data files to process and to be stored in the remote database. Each line of a data file represents a row in the database, but must be formatted before i

2条回答
  •  花落未央
    2021-02-05 19:16

    I know I'm late to the party, but why couldn't you combine all your INSERT statements into a single string, with a semicolon marking the end of each statement? (Warning! Pseudocode ahead...)

    Instead of:

    for each line
      sql_statement="INSERT whatever YOU want"
      echo $sql_statement | psql ...
    done
    

    Use:

    sql_statements=""
    for each line
      sql_statement="INSERT whatever YOU want;"
      sql_statements="$sql_statements $sql_statement"
    done
    echo $sql_statements | psql ...
    

    That way you don't have to create anything on your filesystem, do a bunch of redirection, run any tasks in the background, remember to delete anything on your filesystem afterwards, or even remind yourself what a named pipe is.

提交回复
热议问题