Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql.cursors
- username = 'Bob'
- # Fill in the details with your own connection information, obviously
- connection = pymysql.connect(host='localhost',
- user='user',
- password='password',
- db='db',
- charset='utf8mb4',
- cursorclass=pymysql.cursors.DictCursor)
- # Grab a cursor that uses connection so the database can be interacted with
- cursor = connection.cursor()
- # Set up the query, indicating places for wildcards
- query = 'SELECT * FROM Users WHERE name = %s'
- # Run the query. The second argument is a tuple of arguments that will replace the wildcard(s).
- # (Single arguments don't need the extra parentheses to define them as a tuple - pymysql can figure that out.)
- num_results = cursor.execute(query, username)
- # execute() returns the number of rows found, so you can just use its output value instead of fetchone() or fetchall()
- if num_results > 0:
- print('Username is taken.')
- else:
- print('Username is free.')
Add Comment
Please, Sign In to add comment