【发布时间】:2020-06-11 08:23:38
【问题描述】:
这段 python 代码有问题,因为我不确定如何实现 sql 语句中获得的用户输入。
print("What two companies would you like to compare? ")
Company1 = input("What is the first compaany you would like? ")
Company2 = input("What is the second company you would like to add? ")
comData = [Company1, Company2]
carComData = (comData)
cursor.execute("Select * from Cars where (Car_brand like?) or (Car_brand like?) order by car_id",(carComData))
cursor.execute(sql)
result = cursor.fetchall()
df = pd.DataFrame(result, columns=['Car_id', 'Car_Brand', 'Car_Model', 'Year', 'VIN', 'KmsTravelled', 'Price', 'Dealer_id'])
print (df) '''
【问题讨论】:
-
您到底遇到了什么麻烦?你用的是什么数据库?哪个司机?您在此处发布的示例有一些明显的错误,例如在
cursor.execute(sql)中使用了缺少的变量,以及整个print (df) ''',所以请提供minimal reproducible example。 -
每个数据库驱动程序都有(不幸的是)不同的绑定值语法。
标签: python sql python-3.x spyder