我想在python中查询一个值,并使用该变量检查MySQL中的表。如果MySQL表值与(按用户)输入的名称相同,则应显示正确答案。请帮助
import mysql.connector
mydb = mysql.connector.connect(host = "localhost",user = "root",passwd = "password")
m = mydb.cursor()
m.execute("use project")
m.execute("Select Question_Name from questions where Question_No = 1")
for x in m:
print(x)
m.execute("Select Option_A,Option_B from questions where Question_No = 1")
for x in m:
print(x)
m.execute("Select Option_C,Option_D from questions where Question_No = 1")
for x in m:
print(x)
t = input("Enter your Answer: ")
ans = (t,)
m.execute("Select Correct_Option from questions where Question_No = 1")
if ans==m:
print("Your answer is correct")
else:
print("Wrong")
m.execute("Select * from questions where Question_No = 1")
for x in m:
print(x)
#OUTPUT
(1, 'The weight of diamonds is usually measured in what?', 'A. Tola', 'B. Carat', 'C. Maund', 'D. Troy', 'Carat')
============== RESTART: E:\Yash Class XII\Project\projectdb.py ==============
('The weight of diamonds is usually measured in what?',)
('A. Tola', 'B. Carat')
('C. Maund', 'D. Troy')
Enter your Answer with option: Carat
Wrong
>>>