Advertisement
Guest User

insere

a guest
May 16th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Importanto a biblioteca para comunicacao com o postgreSQL
  3. import psycopg2 # sudo apt-get install python-psycopg2
  4. from datetime import datetime
  5.  
  6. def main():
  7. print('Conectando...')
  8. connect()
  9. now = datetime.now()
  10. currentYear = now.year
  11. currentMonth = now.month
  12. currentDay = now.day
  13. currentHour = now.hour
  14. currentMin = now.minute
  15.  
  16.  
  17. def connect():
  18. con = None
  19. """ Tentanto estabelecer a conexao com o banco """
  20.  
  21. try:
  22. print('Conectando com o banco de dados ...')
  23. con = psycopg2.connect(database="postgres",
  24. user="postgres", password="banco",host='localhost',port=5432)
  25.  
  26. # criando um cursor
  27. cur = con.cursor()
  28.  
  29. # executando um select no postgreSQL
  30. #print('Versao do postgreSQL: ')
  31. #cur.execute('SELECT version()')
  32.  
  33. #clientes = (
  34. # (17,'Sr', 'Afonso', 'da Silva', 'Avenida Acelino de Leao', '89', 'NULL', '68900 300', 'Macapa', 'AP', '3565 1243', '8765 8999' ,1),
  35. # (18,'Sr', 'Afonso', 'da Silva', 'Avenida Acelino de Leao', '89', 'NULL', '68900 300', 'BH', 'MG', '3565 1243', '8765 8999' ,1)
  36. #)
  37.  
  38.  
  39.  
  40. """cur.execute('SELECT id_cliente, nome, sobrenome, endereco FROM aulas.tb_cliente')
  41.  
  42.  
  43. #db_version = cur.fetchone() #aqui so um registro de retorno do select
  44. #print(db_version)
  45.  
  46. #rows = cur.fetchall()
  47. #for row in rows:
  48. # print row[0], row[1],row[2]
  49. #print row"""
  50.  
  51.  
  52. # Inserindo um valor na tabela de cliente
  53. #cur.execute("INSERT INTO aulas.tb_cliente(id_cliente, titulo, nome, sobrenome, endereco, numero, complemento, cep, cidade, estado, fone_fixo, fone_movel, fg_ativo) " +
  54. #"VALUES (17, 'Sr', 'Manoel', 'da Silva', 'Avenida Acelino de Leao', '89', NULL, '68900 300', 'Macapa', 'AP', '3565 1243', '8765 8999' ,1)")
  55.  
  56.  
  57. query = "INSERT INTO projeto.funcionario(matricula, horarioent) VALUES (%s,%s)"
  58. print(query)
  59. cur.executemany(query,Funcionario)
  60. # Atualizando o dados na tabela de cliente
  61.  
  62.  
  63. # fechando a comunicacao com o postgreSQL --- IMPORTANTE!!!
  64.  
  65. con.commit()
  66. cur.close()
  67. except (Exception, psycopg2.DatabaseError) as error:
  68. print(error)
  69.  
  70. finally:
  71. # Ja existe uma conexao e sera fechada
  72. if con is not None:
  73. con.close()
  74. print('Conexao com o banco fechada')
  75.  
  76. if __name__ == "__main__":
  77. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement