30 lines
867 B
Python
30 lines
867 B
Python
'''
|
|
pymysql 更底层封装sql - 游标
|
|
'''
|
|
import pymysql.cursors
|
|
|
|
# Connect to the database
|
|
connection = pymysql.connect(
|
|
host="localhost",
|
|
user="geeker",
|
|
password="geeker",
|
|
database="fastapi_tutor",
|
|
cursorclass=pymysql.cursors.DictCursor,
|
|
)
|
|
|
|
with connection:
|
|
with connection.cursor() as cursor:
|
|
# Create a new record
|
|
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
|
|
cursor.execute(sql, ("webmaster@python.org", "very-secret"))
|
|
|
|
# connection is not autocommit by default. So you must commit to save
|
|
# your changes.
|
|
connection.commit()
|
|
|
|
with connection.cursor() as cursor:
|
|
# Read a single record
|
|
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
|
|
cursor.execute(sql, ("webmaster@python.org",))
|
|
result = cursor.fetchone()
|
|
print(result) |