使用mysql.connector将Python变量用于mysql表

时间:2019-03-28 08:11:43

标签: python mysql mysql-connector-python

我想在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
>>> 

应该正确显示答案

0 个答案:

没有答案