python - popen sql string issue -




using popen run postgresql query. works:

maxid = 10  psql = "psql -d db -u user -t -c 'select id, experiment_id results id > " + maxid + "'"  ssh = subprocess.popen(['ssh', 'me@server', psql],                        shell=false,                        stdout=subprocess.pipe,                        stderr=subprocess.pipe) result = ssh.stdout.readlines() 

when add clause, fails:

psql = "psql -d db -u user -t -c 'select id, experiment_id results status '%completed%' , id > " + maxid + "'"  ['error:  syntax error @ or near "%"\n', 'line 1: ...t_id" results status %completed...\n', '                                                             ^\n'] 

i've tried several quotation alternatives can't seem right.

any appreciated.

td

update: able preserve double-quoted column names across call psql original issue of passing clause in single quotes. solution below:

found hint here: keeping double quotes when passing string popen in c

psqlcmd = 'psql -u choa -d iondb -t -c '  sql = "\"select id, experiment_id, \\\"resultsname\\\" rundb_results status '%completed%' , id > 5;\""  print(psqlcmd) print(sql)  ssh = subprocess.popen(['ssh', 'me@server', psqlcmd, sql],                        shell=false,                        stdout=subprocess.pipe,                        stderr=subprocess.pipe) result = ssh.stdout.readlines() 

i've tried reproduce , managed on shell, doing through python mess. i'll show findings incase helps solve problem.

notice difference double quotes on outside of select statement.

you need either escape set of single quotes, or change double quotes , escape set of double quotes.

[root@server tmp]# /opt/rh/postgresql/bin/psql -d database -u user -c 'select * control option_name '%manage%';' error:  syntax error @ or near "%" line 1: select * control option_name %manage%;                                                      ^   [root@server tmp]# /opt/rh/postgresql/bin/psql -d database -u user -c "select * control option_name '%manage%';"  option_name  | status  --------------+--------  manage_ntpd  | false  manage_dhcp4 | false  manage_dhcp6 | false  manage_named | false (4 rows) 




wiki

Comments

Popular posts from this blog

python - Read npy file directly from S3 StreamingBody -

python - Minimize function with Scipy minimize -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -