Repository navigation
Expand file tree
/
Copy path032_python_postgresql.py
More file actions
85 lines (54 loc) · 2.56 KB
/
Copy path032_python_postgresql.py
File metadata and controls
85 lines (54 loc) · 2.56 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
# MARKDOWN ```
import psycopg2
# Create Connection Object
# ##############################################################################
conn = psycopg2.connect(database="syntaxboard", user = "syntaxboard", password = "syntaxboard", host = "127.0.0.1", port = "5432")
cursor = conn.cursor()
# Executing DDL (Data Definition Language)
# ##############################################################################
# Try to Drop the table
cursor.execute('set search_path="public"')
try:
cursor.execute("drop table ti_test");
except:
pass
# Python pymysql MYSQL Create a table with multiple data types
cursor.execute("create table ti_test ( col1 int, col2 varchar(100), col3 date, col4 text );" )
# python pymysql MYSQL Alter Table to add primary key
cursor.execute("alter table ti_test add constraint ti_test_pk primary key (col1);")
# Executing DML (Data Manipulation Language)
# ##############################################################################
# Python pymysql MYSQL Insert data into the table
cursor.execute("insert into ti_test values (1, 'Test', now(), 'THIS IS CLOB DATA .........');")
cursor.execute("insert into ti_test values (2, 'Test2', now(), 'THIS IS CLOB DATA .........');")
# python pymysql MYSQL Update data in the table
cursor.execute("update ti_test set col2 = '1DATA CHANGE' where col1 = 1;")
# python pymysql MYSQL delete data in the table
cursor.execute("delete from ti_test where col1 = 2;")
cursor.execute("insert into ti_test values (3, 'T1est3', now(), '333 THIS IS CLOB DATA .........');")
cursor.execute("insert into ti_test values (4, 'T1est4', now(), '444 THIS IS CLOB DATA .........');")
conn.commit()
cursor.execute("insert into ti_test values (5, 'T1est5', now(), '555 THIS IS CLOB DATA .........');")
# To RollBack changes
conn.rollback()
# Executing Data Selection
# ##############################################################################
# Fetch One Row
cursor.execute("select col1,col2,col3,col4 from ti_test;")
# Prints the Row count of the cursor
print(cursor.rowcount)
# Fetches a Single Row
rowdata = cursor.fetchone()
print(rowdata)
# Reading multiple datatypes and print to screen
cursor.execute("select col1,col2,col3,col4 from ti_test;")
# use fetchall() to get the list of row data
for l_col1, l_col2, l_col3, l_col4 in cursor.fetchall():
print("l_col1: ", l_col1)
print("l_col2: ", l_col2)
print("l_col3: ", l_col3)
print("l_col4: ", l_col4)
# Close the Cursor
cursor.close()
# Close the Connection
conn.close()