此代码在我的数据库中搜索手机型号(假设为“iPhone”),我希望也可以选择按制造商(Apple)进行搜索。我还想按价格搜索,而不是价格范围,而只是价格 (20 英镑、30 英镑、35 英镑、40 英镑)。
基本上,我希望代码能够通过以下任一方式搜索我的数据库:
*型号
*制作
*每月价格
*预付价格
这是我的代码:
#!/Python27/python
import mysql.connector
def SearchPhoneByPhone(Model):
conn = mysql.connector.connect(user='root', \
host='127.0.0.1', database ='phone')
cur = conn.cursor()
query = "SELECT phone.Model, phone.Make, tariff.Duration, price.PriceUpfront, price.PriceperMonth FROM phone, tariff, price \
WHERE phone.Phone_ID = price.Phone_ID AND tariff.Tariff_ID = price.Tariff_ID \
AND phone.Model LIKE '%" + Model + "%'"
cur.execute(query)
Model = "device"
print "This " + Model + " has the following tariffs:"
for (Model, Make, Duration, PriceUpfront, PriceperMonth) in cur.fetchall():
print (Make + " ") + Model + " (Duration of the Contract: " + Duration + ")" + " (Price Upfront: " + PriceUpfront + ")" + " (Price per Month: " + PriceperMonth + ")"
print ""
cur.close()
conn.close()
SearchPhoneByPhone('iPhone')
它返回结果:
This device has the following tariffs:
Apple iPhone 5S (Duration of the Contract: 24 Months) (Price Upfront: £ 99) (Price per Month: £ 20)
Apple iPhone 6 (Duration of the Contract: 24 Months) (Price Upfront: £ 109) (Price per Month: £ 20)
Apple iPhone 6 Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 119) (Price per Month: £ 20)
Apple iPhone 6S (Duration of the Contract: 24 Months) (Price Upfront: £ 129) (Price per Month: £ 20)
Apple iPhone 6S Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 139) (Price per Month: £ 20)
Apple iPhone 5S (Duration of the Contract: 24 Months) (Price Upfront: £ 99) (Price per Month: £ 30)
Apple iPhone 6 (Duration of the Contract: 24 Months) (Price Upfront: £ 109) (Price per Month: £ 30)
Apple iPhone 6 Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 119) (Price per Month: £ 30)
Apple iPhone 6S (Duration of the Contract: 24 Months) (Price Upfront: £ 129) (Price per Month: £ 30)
Apple iPhone 6S Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 139) (Price per Month: £ 30)
Apple iPhone 5S (Duration of the Contract: 24 Months) (Price Upfront: £ 99) (Price per Month: £ 35)
Apple iPhone 6 (Duration of the Contract: 24 Months) (Price Upfront: £ 109) (Price per Month: £ 35)
Apple iPhone 6 Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 119) (Price per Month: £ 35)
Apple iPhone 6S (Duration of the Contract: 24 Months) (Price Upfront: £ 129) (Price per Month: £ 35)
Apple iPhone 6S Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 139) (Price per Month: £ 35)
Apple iPhone 5S (Duration of the Contract: 24 Months) (Price Upfront: £ 99) (Price per Month: £ 40)
Apple iPhone 6 (Duration of the Contract: 24 Months) (Price Upfront: £ 109) (Price per Month: £ 40)
Apple iPhone 6 Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 119) (Price per Month: £ 40)
Apple iPhone 6S (Duration of the Contract: 24 Months) (Price Upfront: £ 129) (Price per Month: £ 40)
Apple iPhone 6S Plus (Duration of the Contract: 24 Months) (Price Upfront: £ 139) (Price per Month: £ 40)
如果我尝试按品牌搜索:
SearchPhoneByPhone('Apple')
它返回一个空白:
This device has the following tariffs:
与价格相同:
前期价格:
SearchPhoneByPhone('£99')
每月价格:
SearchPhoneByPhone('£20')
谢谢!
最佳答案
更改phone.Model LIKE '%" + Model + "%'
在查询的 WHERE 子句中。
query = """SELECT phone.Model, phone.Make, tariff.Duration, price.PriceUpfront,
price.PriceperMonth
FROM phone, tariff, price
WHERE phone.Phone_ID = price.Phone_ID
AND tariff.Tariff_ID = price.Tariff_ID
AND (phone.Model LIKE %s
OR phone.Make LIKE %s
OR price.PriceperMonth = %s)"""
args = ['%{}%'.format(Model)]*2 + [Model]
cur.execute(query, args)
- 您可以使用三引号来表示
query
多行字符串。一点缩进将使 SQL 更具可读性。 - Use parametrized SQL (1) 防止 SQL 注入(inject),(2) 允许为您正确引用参数。
%s
是占位符,指示要替换参数的位置。args
列表保存要替换%s
的值占位符。使用参数化 SQL,参数将作为调用cur.execute
中的第二个参数传递给游标。 -- 如cur.execute(query, args)
. - 由于变量名为
Model
不再仅代表模型,您可能需要将其更改为其他变量名称,例如keyword
或search_term
。 - PEP8 style-guide还建议函数和变量名称采用小写形式,以便轻松区分驼峰式命名的类名称。
- 使用str.format可以让你的字符串更容易阅读。
所以把它们放在一起,你可能会得到类似的结果:
import mysql.connector
def SearchPhoneByPhone(search_term):
conn = mysql.connector.connect(user='root', host='127.0.0.1', database ='phone')
cur = conn.cursor()
query = """SELECT phone.Model, phone.Make, tariff.Duration, price.PriceUpfront,
price.PriceperMonth
FROM phone, tariff, price
WHERE phone.Phone_ID = price.Phone_ID
AND tariff.Tariff_ID = price.Tariff_ID
AND (phone.Model LIKE %s
OR phone.Make LIKE %s
OR price.PriceperMonth = %s)"""
args = ['%{}%'.format(search_term)]*2 + [search_term]
cur.execute(query, args)
fmt = '{} {} (Duration of the Contract: {}) (Price Upfront: {}) (Price per Month:{})'
print "This device has the following tariffs:"
for (model, make, duration, price_up_front, price_per_month) in cur.fetchall():
print fmt.format(model, make, duration, price_up_front, price_per_month)
print ""
cur.close()
conn.close()
SearchPhoneByPhone('iPhone')
关于python - 此代码在我的数据库中搜索手机型号,我希望能够选择按品牌进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34897749/