-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresql.py
58 lines (46 loc) · 1.65 KB
/
postgresql.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
52
53
54
55
56
57
58
import psycopg2
import sys
dsn_database = "testdb"
dsn_hostname = "localhost"
dsn_port = "5432"
dsn_uid = "xxxxxx"
dsn_pwd = "xxxxxx"
try:
conn_string = "host="+dsn_hostname+" port="+dsn_port+" dbname="+dsn_database+" user="+dsn_uid+" password="+dsn_pwd
print("Connecting to database\n ->%s" % (conn_string))
conn=psycopg2.connect(conn_string)
print("Connected!\n")
except:
print("Unable to connect to the database.")
cursor = conn.cursor()
cursor.execute("""SELECT datname from pg_database""")
rows = cursor.fetchall()
print("\nShow me the databases:\n")
for row in rows:
print (" ", row[0])
# Create the table
cursor.execute("DROP TABLE IF EXISTS Cars")
cursor.execute("CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price INT)")
# Insert Data into table
cursor.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
cursor.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
cursor.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
cursor.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
cursor.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
cursor.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
cursor.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
cursor.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")
conn.commit()
cursor.execute("""SELECT * from Cars""")
rows = cursor.fetchall()
print("\nShow me the Cars:\n")
import pprint
pprint.pprint(rows)
for row in rows:
print(" Number=", row[0] ," Name=", row[1]," Price", row[2])
fout = open('cars.csv', 'w')
cursor.copy_to(fout,'cars',sep=",")
fin=open('cars.csv','r')
cursor.copy_from(fin,'cars',sep=",")
conn.commit()
conn.close()