首页 > 解决方案 > 如何使用python从mysql数据库的信息中执行用户输入

问题描述

def choice_2():
    number = int(input("Enter year:   "))
    if not conn:
        connect()      

    query = "SELECT * from country IndepYear"    
    with conn:
        cursor = conn.cursor()
        cursor.execute(query, (number))

        country = cursor.fetchall()

        print()

在 anaconda 应用程序中给我一个错误,为什么?

标签: python

解决方案


在这个解决您的问题的方法中,将您的输入放在方括号 (,) 中,因此结果将如下所示:

user_input = (raw_input("Enter year:"), )

别忘了加逗号,

完全你有

import MySQLdb
servername = "localhost";
username = "username";
password = "password";
conn = MySQLdb.connect(user=username,host=servername, passwd=password, db=db)
cursor = conn.cursor()
user_input = (raw_input("Enter year:"), )
query= ("SELECT * from country IndepYear")
cursor.execute(query,user_input)

推荐阅读