A bit of a vague title, I will explain.
I am writing an SQL script to create an insert statement for each row of a table in my database, purely to be able to apply that
I wrote a python script based on @intgr answer to construct the select statement. It takes comma separated list of columns from stdin (use -).
I wanted to use sqlparse but I couldn't understand how to use that lib.
import fileinput
names = ' '.join(fileinput.input())
ns = [x.strip() for x in names.split(',')]
quoted = ['quote_nullable(' + x + ')' for x in ns]
insert = "SELECT 'INSERT INTO ( " + (', ').join(ns) + " ) VALUES(' || " + (" || ',' || ").join(quoted) + " || ');' FROM "
print insert
A gist of the script is here: https://gist.github.com/2568047
- 热议问题