-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectpgsql.py
More file actions
72 lines (54 loc) · 1.62 KB
/
connectpgsql.py
File metadata and controls
72 lines (54 loc) · 1.62 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
#/usr/bin/env python
# -*- coding: utf-8 -*-
import psycopg2
import xlrd
DSN = "host=161.196.204.6 dbname=prueba user=postgres password=postgres port=5433"
def leer_datos():
con = psycopg2.connect(DSN) # se crea la conexión
cur = con.cursor() # se crea un cursor
##########################
# Open the workbook and define the worksheet
book = xlrd.open_workbook("Libros.xls")
sheet = book.sheet_by_name("Hoja1")
# Get the cursor, which is used to traverse the database, line by line
database = psycopg2.connect (DSN)
#
query = """INSERT INTO malave_final (Titulo, Autor, Tipo, Paginas) VALUES (%s, %s, %s, %s)"""
#
for r in range(1, sheet.nrows):
Titulo = sheet.cell(r,).value
Autor = sheet.cell(r,1).value
Tipo = sheet.cell(r,2).value
Paginas = sheet.cell(r,3).value
# Assign values from each row
values = (Titulo, Autor, Tipo, Paginas)
# Execute sql Query
cur.execute(query, values)
# Close the cursor
cur.close()
# Commit the transaction
database.commit()
# Close the database connection
database.close()
# Print results
print ""
print "All Done! Bye, for now."
print ""
print "I "
########################
"""
def leer_datos():
con = psycopg2.connect(DSN) # se crea la conexión
cur = con.cursor() # se crea un cursor
query = "INSERT INTO malave_final(Titulo,Autor,Tipo,Paginas) VALUES ('Titulo','autor','tipo',200);"
cur.execute(query)
con.commit()
query = "SELECT * FROM malave_final;"
cur.execute(query)
for id in cur.fetchall():
print (id)
cur.close()
con.close()
if __name__== "__main__":
leer_datos();
"""