【问题标题】:Cannot access data from database using specific id无法使用特定 ID 访问数据库中的数据
【发布时间】:2017-10-20 08:57:35
【问题描述】:
import pymysql
import os

conn =pymysql.connect(host='localhost',database='pyp',user='root',password='')
a = conn.cursor()
h= raw_input('enter your id')
sql='SELECT * from report WHERE Id = h'
a.execute(sql)
data=a.fetchall()

print(data)
conn.close()

【问题讨论】:

    标签: python python-2.7 pymysql


    【解决方案1】:

    问题是您的查询是:

    SELECT * from report WHERE Id = <b>h</b>

    因此它将h 解释为一个MySQL 列h(可能甚至不存在)

    除非您指定,否则 Python 不会自动将 h 替换为变量 h

    您可以使用以下方法解决此问题:

    sql='SELECT * from report WHERE Id = %s'
    a.execute(sql,(h,))

    因此,您使用%s 指定一个参数,然后使用(列表)值调用.execute(..)

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 2021-11-08
      • 1970-01-01
      • 2018-02-21
      • 2022-12-12
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多