-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsert_csv_posgre.py
36 lines (30 loc) · 1.04 KB
/
insert_csv_posgre.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
# python 3
import csv
import psycopg2
def main():
with open('example.csv') as f:
"""
example.csv :
id email name \
0 0 [email protected] Joseph Kirby
1 1 [email protected] Erin Figueroa
2 2 [email protected] Leon Matthews
address
0 3594 Fox Ford Apt. 192 West Kristen GA 22838-8977
1 64763 Li Meadows Apt. 554 New Marcoton MA 9901...
2 91144 Hamilton Manors Suite 421 Ronaldland WA ...
"""
reader = csv.reader(f)
next(reader)
rows = [row for row in reader]
conn = psycopg2.connect("dbname=dq user=dq")
cur = conn.cursor()
for row in rows:
cur.execute("INSERT INTO users VALUES (%s, %s, %s, %s)", row)
conn.commit()
cur.execute('SELECT * FROM users')
users = cur.fetchall()
conn.close()
if __name__ == '__main__':
main()