python 2.7 - Updating entire column with data from tuple in PostgreSQL(psycopg2) -


first of all, there script:

import psycopg2 import sys  data = ((160000,),         (40000,),         (75000,),         )  def main():     try:         connection = psycopg2.connect("""host='localhost' dbname='postgres'                                          user='postgres'""")         cursor = connection.cursor()         query = "update planes set price=%s"         cursor.executemany(query, data)         connection.commit()      except psycopg2.error, e:         if connection:             connection.rollback()         print 'error:{0}'.format(e)      finally:         if connection:             connection.close()  if __name__ == '__main__':     main() 

this code works of course, not in way want. updates entire column 'price' good, updates use of last value of 'data'(75000).

(1, 'airbus', 75000, 'public') (2, 'helicopter', 75000, 'private') (3, 'falcon', 75000, 'military') 

my desire output like:

(1, 'airbus', 160000, 'public') (2, 'helicopter', 40000, 'private') (3, 'falcon', 75000, 'military') 

now, how can fix it?

without setting database on machine debug, can't sure, appears query issue. when execute

update planes set price=%s

i think updating entire column value being iterated on data tuple. instead, might need tuple dictionary

({'name':'airbus', 'price':160000}, {'name':'helicopter', 'price':40000}...)

and change query to

"""update planes set price=%(price)s name=%(name)s""".

see bottom of this article similar formulation. check indeed issue, execute query once (cursor.execute(query)) , bet full price column filled first value in data tuple.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -