Guest User

Untitled

a guest
Jan 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import pymysql.cursors
  2.  
  3. username = 'Bob'
  4.  
  5. # Fill in the details with your own connection information, obviously
  6. connection = pymysql.connect(host='localhost',
  7. user='user',
  8. password='password',
  9. db='db',
  10. charset='utf8mb4',
  11. cursorclass=pymysql.cursors.DictCursor)
  12.  
  13. # Grab a cursor that uses connection so the database can be interacted with
  14. cursor = connection.cursor()
  15.  
  16. # Set up the query, indicating places for wildcards
  17. query = 'SELECT * FROM Users WHERE name = %s'
  18.  
  19. # Run the query. The second argument is a tuple of arguments that will replace the wildcard(s).
  20. # (Single arguments don't need the extra parentheses to define them as a tuple - pymysql can figure that out.)
  21. num_results = cursor.execute(query, username)
  22.  
  23. # execute() returns the number of rows found, so you can just use its output value instead of fetchone() or fetchall()
  24. if num_results > 0:
  25. print('Username is taken.')
  26. else:
  27. print('Username is free.')
Add Comment
Please, Sign In to add comment