-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
44 lines (29 loc) · 890 Bytes
/
demo.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
import psycopg2
connection = psycopg2.connect('dbname=example')
cursor = connection.cursor()
cursor.execute('DROP TABLE IF EXISTS table2;')
cursor.execute('''
CREATE TABLE table2 (
id INTEGER PRIMARY KEY,
completed BOOLEAN NOT NULL DEFAULT False
);
''')
cursor.execute('INSERT INTO table2 (id, completed) VALUES (%s, %s);', (1, True))
SQL = 'INSERT INTO table2 (id, completed) VALUES (%(id)s, %(completed)s);'
data = {
'id': 2,
'completed': False
}
cursor.execute(SQL, data)
cursor.execute('INSERT INTO table2 (id, completed) VALUES (%s, %s);', (3, True))
cursor.execute('SELECT * from table2;')
result = cursor.fetchmany(2)
print('fetchmany', result)
result2 = cursor.fetchone()
print('fetchone ' , result2)
cursor.execute('SELECT * from table2;')
result3 = cursor.fetchone()
print('fetchone ' , result3)
connection.commit()
connection.close()
cursor.close()