1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| # coding: utf-8
'''
Created on 16 juin 2018
@author: moi
'''
import psycopg2 # Acc�s PostgreSQL
import psycopg2.extras # Goodies PostgreSQL
try:
conn = psycopg2.connect(dbname="toto", user="toto", password="toto")
cur = conn.cursor()
SQlcode ="""
INSERT INTO public.distributors
(dname)
VALUES('aa')
RETURNING did;
"""
try:
cur.execute(SQlcode)
numerosequence = cur.fetchone()[0]
print("numero sequence:" + str(numerosequence))
conn.commit()
except psycopg2.Error as e:
print("Opération d'insertion impossible")
print(e)
SQlcode ="""
INSERT INTO public.distributors
(dname)
VALUES ( %s )
RETURNING did;
"""
try:
dname = 'test'
# execute the INSERT statement
cur.execute(SQlcode, (dname,))
numerosequence = cur.fetchone()[0]
print("numero sequence:" + str(numerosequence))
conn.commit()
except psycopg2.Error as e:
print("Opération d'insertion impossible")
print(e)
try:
cur.execute("""SELECT did, dname
FROM public.distributors;""")
rows = cur.fetchall()
for row in rows:
print(" ", row[1][1])
except psycopg2.Error as e:
print(" Selection impossible des données de la table distributors ")
print(e)
cur.close()
conn.close()
except psycopg2.Error as e:
print("Je ne parviens pas à me connecter à la base de données.")
print(e) |
Partager