Home How to use INSERT query to avoid duplicate entries in postgresql database tables
Reply: 0

How to use INSERT query to avoid duplicate entries in postgresql database tables

kiruthika
1#
kiruthika Published in 2017-12-07 14:21:08Z

Hi..while using the follwing code i am getting duplicate entries in my table.. Please suggest some method to avoid such duplicate entries..!!

Is there any other mode of INSERT query to acheive duplication free tables..???

import psycopg2

def connect():
    con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432' ")
    cur=con.cursor()
    cur.execute("CREATE TABLE if not exists books(id SERIAL PRIMARY KEY,title TEXT NOT NULL,author TEXT NOT NULL,year integer NOT NULL,isbn integer NOT NULL)")
    con.commit()
    con.close()

def insert(title,author,year,isbn):
    con=psycopg2.connect("dbname='book_store' user='postgres' password='5283' host='localhost' port='5432'")
    cur=con.cursor()
    cur.execute("INSERT INTO books(title,author,year,isbn) VALUES(%s,%s,%s,%s)",(title,author,year,isbn))
    con.commit()
    con.close()


connect()
insert("the sun","helen",1997,23456777)
insert("the sun","helen",1997,23456777)

Here the same entry gets added again..where i want my code to neglect such duplication..!!!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303989 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO