python - 此代码在我的数据库中搜索手机型号,我希望能够选择按品牌进行搜索

标签 python mysql python-2.7

此代码在我的数据库中搜索手机型号(假设为“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不再仅代表模型,您可能需要将其更改为其他变量名称,例如 keywordsearch_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/

相关文章:

python - 简单: df. A = sr中隐藏的地雷

python - 手动计算性能矩阵

python - 有效地对列表中的元素进行加权计数

mysql - 我如何在 mysql 中插入多个值并避免重复

MySQL 'REPEATABLE READ' 事务异常行为

python - 确保 COM 连接启动的进程被终止

python - 在database.ini文件中找不到节postgresql

python - 从 paramiko 获取 PID

php - 未在 phpmailer 脚本中输入 mysql 数据库的字段

python - 当我在 python 3.6 中使用 HMAC 时,我无法将 str 连接到字节