-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (39 loc) · 1.81 KB
/
main.py
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
import psycopg2
#--------------------------------------------!--------------------------------------------
#connect to the db
con = psycopg2.connect(
# don't forget write your data !!
host = "localhost-name",
database="db-name",
user = "server-yser",
password = "your-password")
#--------------------------------------------!--------------------------------------------
#cursor
cur = con.cursor()
#--------------------------------------------!--------------------------------------------
#add a new employee
#cur.execute("insert into employees (name,age,departement) values ('jhony',34,'IT') ;")
#--------------------------------------------!--------------------------------------------
#update employee
#cur.execute("update employees set name='linda' , age=27 , departement='HR' where id =2 ;")
#--------------------------------------------!--------------------------------------------
#delete employee
cur.execute("delete from employees where id=4 ;")
#--------------------------------------------!--------------------------------------------
#execute query
cur.execute("select * from employees")
#--------------------------------------------!--------------------------------------------
#show data
rows = cur.fetchall()
for r in rows:
print (f"id : {r[0]} , name : {r[1]}, age : {r[2]}, departement : {r[3]}")
#--------------------------------------------!--------------------------------------------
#commit the transcation
con.commit()
#--------------------------------------------!--------------------------------------------
#close the cursor
cur.close()
#--------------------------------------------!--------------------------------------------
#close the connection
con.close()
#--------------------------------------------!--------------------------------------------