python - 如何使用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 应用程序中给我一个错误,为什么?
解决方案
在这个解决您的问题的方法中,将您的输入放在方括号 (,) 中,因此结果将如下所示:
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)
推荐阅读
- html - 如何仅使用 HTML 制作此表
- mysql - 从 INNER JOIN SQL 中的第二个表返回第一个索引
- sql - 在 SQL 查询中排序
- java - 每次出现某个角色后,我怎样才能打破一个段落?
- flutter - 降级颤振版本以避免空安全
- c# - 无法使用 C# 更新 Unity2D 中的刚体
- amazon-web-services - Runway X Serverless Framework:使用 Runway for CICD 从无服务器仪表板使用环境变量进行部署
- spring-boot - 如何优雅地关闭 Spring Boot 应用程序
- java - 如何在 JSON 数组中发送特定数据
- python - 使用 lambda 的阶乘函数