Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Utilizando o PostgreSQL no Python
- Instalar o módulo psycopg2
- pip install psycopg2
- Curso de Python 3
- https://www.udemy.com/curso-de-python3/
- Diego Mendes Rodrigues
- '''
- import psycopg2
- conexao = psycopg2.connect(dbname='dbexemplo',
- user='demo',
- password='DeM@123@Po',
- host='127.0.0.1',
- port='5432')
- cursor = conexao.cursor()
- cursor.execute('DROP TABLE pessoas;');
- cursor.execute('CREATE TABLE pessoas (codigo SERIAL PRIMARY KEY, nome TEXT, idade INTEGER);')
- cursor.execute('INSERT INTO pessoas (nome, idade) VALUES (%s, %s)',('Bruna Mendes', 29))
- cursor.execute('INSERT INTO pessoas (nome, idade) VALUES (%s, %s)',('Julia Neau', 2))
- cursor.execute('INSERT INTO pessoas (nome, idade) VALUES (%s, %s)',('Regina Marcia', 55))
- conexao.commit()
- cursor.execute('SELECT * FROM pessoas;')
- for linha in cursor.fetchall():
- print('\nCódigo.: {} '.format(linha[0]))
- print('Nome...: {} '.format(linha[1]))
- print('Idade..: {} '.format(linha[2]))
- cursor.close()
- conexao.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement