v0.1 fastapi: route + model + orm
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user