Friday, 15 August 2014

sql - Dynamic conditional insert in postgresql -



sql - Dynamic conditional insert in postgresql -

i attempting define conditional insert in postgresql, on index on 3 columns (which gives uniqueness). i'm trying follow next illustration postgresql documentation:

insert example_table (id, name) select 1, 'john' not exists ( select id example_table id = 1 );

for basic select not exists structure. if index varies, i.e. want prevent insert if there exists selection in table id=index value of current pre-insert row, how implement this? here current (wrong) code:

insert = ( "insert table (index,a,b,c,d,e)" "select * table not exists (select * table index=index)"); cur.execute(insert,data)

for clarity, index defined on info columns (a,b,c), info row of (index,a,b,c,d,e), , i'm wrapping in psycopg2. have searched reply while, haven't been able adapt problem yet.

insert t1 (a, b, c, d, e) select a, b, c, d, e t2 not exists ( select 1 t1 = t2.a , b = t2.b , c = t2.c );

in python easier , cleaner utilize triple quote raw string

insert = """ insert t1 (a, b, c, d, e) select a, b, c, d, e t2 not exists ( select 1 t1 = t2.a , b = t2.b , c = t2.c ); """

sql postgresql psycopg2

No comments:

Post a Comment