Ria
Ria

Reputation: 167

PostgreSql and Python

I am using python and postgresql. I have a table with 6 column. One id and 5 entries. I want to copy the id and most repeated entry in 5 entries to a new table.

I have done this:

import psycopg2
connection=psycopg2.connect("dbname=homedb user=ria")
cursor=connection.cursor()
l_dict= {'licence_id':1}
cursor.execute("SELECT * FROM im_entry.usr_table")
rows=cursor.fetchall()


cursor.execute("INSERT INTO im_entry.pr_table (image_1d) SELECT  image_1d  FROM im_entry.usr_table")



for row in rows:

   p = findmax(row) #to get most repeated entry from first table
   .................
   .................

Then how can I enter this p value to the new table?

Please help me

Upvotes: 1

Views: 750

Answers (1)

axaroth
axaroth

Reputation: 231

p is a tuple so you can create a new execute with the INSERT statement passing the tuple (or part):

cursor.execute("INSERT INTO new_table (x, ...) VALUES (%s, ...)", p)

where:

  • (x, ....) contains the column names
  • (%s, ...) %s is repeated for each column

Upvotes: 1

Related Questions