(Since it is a memorandum, it is easy. For more information, refer to Documentation etc.)
-When connecting to DB with sqlalchemy and issuing a query with execute () It will return the'sqlalchemy.engine.result.ResultProxy' class object
-In other words, some ingenuity is required to return the value obtained from the DB with int or str.
↓ ↓ ↓ For example, like this ↓ ↓ ↓
from sqlalchemy import create_engine
# As an aside, let's do type annotation properly. Don't imitate this function declaration
def fetchUserIdFromDB(userName):
#DB connection
engine = create_engine('sqlite:///app.db')
userId = []
with engine.connect() as con:
# Query issuance
rows = con.execute("select USER_ID from user_table where USER_NAME='{}'".format(userName))
print(type(rows)) # -> <class 'sqlalchemy.engine.result.ResultProxy'>
for row in rows:
# Make a dictionary line by line ...
userDict = dict(row)
Int # value and push to array!
userId.push(int(liquorDict['USER_ID']))
return userId
Recommended Posts