【发布时间】:2021-01-01 18:35:03
【问题描述】:
我的 python 项目的以下代码似乎不起作用
import mysql.connector as sql
cxn=sql.connect(user='adithyan',password='vinod123',database='workout2')
cursor=cxn.cursor()
while True:
try:
l=[ ]
studentid =int(input("Please enter your student_id"))
a = 'select * from student_info where Student_ID = %studentid'
cursor.execute(a)
for i in cursor:
l.append(i)
except:
print("Student_id not found try again")
MySQL连接没有问题,select语句也没有问题(即我在python中独立运行时查询运行正确)但是 看来我不能在 SQL 查询中使用 python 中的变量。另外,请建议 如果可能的话还有其他选择!!
干杯, 阿提提安
P.S:-这是我的家庭作业不。为了学习 MySQL,我观看了 YouTube,在我的编码课程中,我想到了做一个简单的 python 项目。那是我遇到这个错误的时候
【问题讨论】:
-
请注意,在“l.append(i)”之后的“try”语句中,我们可以使用“print(l)”语句
标签: python mysql python-3.x mysql-connector mysql-connector-python