Advertisement
Guest User

Untitled

a guest
Oct 19th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. import psycopg2
  2. from psycopg2.extras import RealDictCursor
  3.  
  4.  
  5. dsn = """host='localhost' port='5432'
  6. dbname='mydb' user='pg' password='pg'"""
  7. con = psycopg2.connect(dsn, cursor_factory=RealDictCursor)
  8.  
  9. create_table = '''
  10. drop table if exists users;
  11. create table users (
  12. id serial primary key not null,
  13. username varchar(255) not null
  14. );
  15. '''
  16.  
  17. insert_data = '''
  18. insert into users (id, username)
  19. values (%(id)s,%(username)s);
  20. '''
  21.  
  22.  
  23. with con.cursor() as cursor:
  24. cursor.execute(create_table)
  25. cursor.execute(insert_data, {'id': 1, 'username': 'fred'})
  26. fred = cursor.execute("select * from users where username == 'fred';")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement