sql - Psycopg2 Python Inserting List -


i'm trying insert list sql query via psycopg2 every time see's ' doubles it...

heres code:

for line in listfile:     if countligne == 1:         idlist = "'" + (line[1].replace('"', '')) + "', "         countligne += 1     elif countligne < nbrligne:         idlist += "'" + (line[1].replace('"', '')) + "', "         countligne += 1     else:         idlist += (line[1].replace('"', '') + "'")      break print(idlist) print(type(idlist)) ftdsql = ( '''with ftd (     select m.event_user, m.event_ts, m.revenue, rank() on (partition            m.event_user order m.event_ts) order_purchase     agg_monetization m     revenue not null ) select distinct ftd.event_user, sum(ftd.revenue) ftd order_purchase = 1 , ftd.event_user in (%s) group ftd.event_user, ftd.event_ts ''' ) cursor.execute(ftdsql, [idlist]) print(cursor.query) 

the list: '849cf768-41ea-4ed0-9861-779369d3eede', '10ad8dca-b4e6-4be5-93d3-b7fb88b1668a'

the result : , ftd.event_user in ('''849cf768-41ea-4ed0-9861-779369d3eede'', ''10ad8dca-b4e6-4be5-93d3-b7fb88b1668a'', ''863c3eaf-d98d-4f6a-bb97-8756750e7a09''

thanks !!

pyscopg2 automatically converts tuples sql list. take on adaptation of python values sql types. need change list tuple , pass it.

ftdsql = '''   ftd (     select        m.event_user, m.event_ts, m.revenue,        rank() on (partition m.event_user order m.event_ts) order_purchase     agg_monetization m     revenue not null   )   select     distinct ftd.event_user, sum(ftd.revenue)   ftd       order_purchase = 1   ,     ftd.event_user in %s     -- parenthesis should added automatically    group ftd.event_user, ftd.event_ts ''' ) cursor.execute(ftdsql, (tuple(idlist),)) 

btw on other hand python lists default converted postgres array


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -