Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- from psycopg2.extras import RealDictCursor
- dsn = """host='localhost' port='5432'
- dbname='mydb' user='pg' password='pg'"""
- con = psycopg2.connect(dsn, cursor_factory=RealDictCursor)
- create_table = '''
- drop table if exists users;
- create table users (
- id serial primary key not null,
- username varchar(255) not null
- );
- '''
- insert_data = '''
- insert into users (id, username)
- values (%(id)s,%(username)s);
- '''
- with con.cursor() as cursor:
- cursor.execute(create_table)
- cursor.execute(insert_data, {'id': 1, 'username': 'fred'})
- fred = cursor.execute("select * from users where username == 'fred';")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement