如何在sql语句中使用来自python的两个用户输入?

z9gpfhce  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(319)

这段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)  '''
xeufq47z

xeufq47z1#

试试这个

cursor.execute("Select * from Cars where (Car_brand like %s) or (Car_brand like %s) order by car_id", (Company1, Company2))

你的问题是你的参数在一个列表而不是元组中。

相关问题