0

我有一个 uni 的任务,我碰壁了,我似乎无法让它工作,无论我尝试什么。

因此,本次作业的重点是从 Python 访问 MySQL 数据库,以及查看和排列数据。该数据库名为 inb104,存在两个表,cars_for_sale 和 car_details。

这是我的代码

def top_N_expensive(num_of_highest):
connection = MySQLdb.connect(host='localhost', user='root', passwd='root',\
                                                                 db='inb104')
cursor = connection.cursor()
sql = "SELECT cars_for_sale.make, cars_for_sale.model, +'$'+car_details.price \
            FROM cars_for_sale, car_details WHERE cars_for_sale.CarId = car_details.CarId \
            ORDER BY price DESC,price LIMIT " + str(num_of_highest)
cursor.execute(sql) 
rows = cursor.fetchall()
for row in rows:
    print row[0], row[1], row[2]
cursor.close()
connection.close()

现在,当我运行此代码时,测试用例失败,这只是其中一个测试用例,但其余的都是相同的

File "__main__", line 4, in __main__
Failed example:
top_N_expensive(10)
Expected:
VOLKSWAGEN GOLF $1300000
MERCEDES-BENZ SL $329990
BMW 3 $299990
MERCEDES-BENZ SL $249990
PORSCHE 911 $249990
MERCEDES-BENZ SL $224990
PORSCHE 911 $219990
PORSCHE 911 $190000
PORSCHE 911 $184990
BMW M5 $180000
Got:
VOLKSWAGEN GOLF 1300000.0
MERCEDES-BENZ SL 329990.0
BMW 3 299990.0
MERCEDES-BENZ SL 249990.0
PORSCHE 911 249990.0
MERCEDES-BENZ SL 224990.0
PORSCHE 911 219990.0
PORSCHE 911 190000.0
PORSCHE 911 184990.0
BMW M5 180000.0

如您所见,它失败了,因为末尾有一个零,而开头没有 $,有人知道问题所在吗?

编辑:添加此新代码后

for row in rows:
    print "%s %s $%d" % (row[0], row[1], row[2]) 

我得到了 3/4 个测试用例通过,我不知道为什么最后一个没有通过,但这是测试用例转储

Trying:
top_N_expensive(10)
Expecting:
VOLKSWAGEN GOLF $1300000
MERCEDES-BENZ SL $329990
BMW 3 $299990
MERCEDES-BENZ SL $249990
PORSCHE 911 $249990
MERCEDES-BENZ SL $224990
PORSCHE 911 $219990
PORSCHE 911 $190000
PORSCHE 911 $184990
BMW M5 $180000

Warning (from warnings module):
File "Z:\Documents\top_N_expensive.py", line 82
cursor.execute(sql)
Warning: Truncated incorrect DOUBLE value: '$'
ok
Trying:
top_N_expensive(15)
Expecting:
VOLKSWAGEN GOLF $1300000
MERCEDES-BENZ SL $329990
BMW 3 $299990
MERCEDES-BENZ SL $249990
PORSCHE 911 $249990
MERCEDES-BENZ SL $224990
PORSCHE 911 $219990
PORSCHE 911 $190000
PORSCHE 911 $184990
BMW M5 $180000
MERCEDES-BENZ E55 $179990
MERCEDES-BENZ CLS $179990
PORSCHE 911 $165000
PORSCHE 911 $164900
PORSCHE 911 $161950
**********************************************************************
File "__main__", line 17, in __main__
Failed example:
top_N_expensive(15)
Expected:
VOLKSWAGEN GOLF $1300000
MERCEDES-BENZ SL $329990
BMW 3 $299990
MERCEDES-BENZ SL $249990
PORSCHE 911 $249990
MERCEDES-BENZ SL $224990
PORSCHE 911 $219990
PORSCHE 911 $190000
PORSCHE 911 $184990
BMW M5 $180000
MERCEDES-BENZ E55 $179990
MERCEDES-BENZ CLS $179990
PORSCHE 911 $165000
PORSCHE 911 $164900
PORSCHE 911 $161950
Got:
VOLKSWAGEN GOLF $1300000
MERCEDES-BENZ SL $329990
BMW 3 $299990
MERCEDES-BENZ SL $249990
PORSCHE 911 $249990
MERCEDES-BENZ SL $224990
PORSCHE 911 $219990
PORSCHE 911 $190000
PORSCHE 911 $184990
BMW M5 $180000
MERCEDES-BENZ CLS $179990
MERCEDES-BENZ E55 $179990
PORSCHE 911 $165000
PORSCHE 911 $164900
PORSCHE 911 $161950
Trying:
top_N_expensive(1)
Expecting:
VOLKSWAGEN GOLF $1300000
ok
Trying:
top_N_expensive(0)
Expecting nothing
ok
1 items had no tests:
__main__.top_N_expensive
**********************************************************************
1 items had failures:
1 of   4 in __main__
4 tests in 2 items.
3 passed and 1 failed.
***Test Failed*** 1 failures.

所以,不知道为什么现在失败了,很奇怪。

这是更新的查询

def top_N_expensive(num_of_highest):
connection = MySQLdb.connect(host='localhost', user='root', passwd='root',\
                                                                 db='inb104')
cursor = connection.cursor()
sql = "SELECT cars_for_sale.make, cars_for_sale.model, +'$'+car_details.price \
            FROM cars_for_sale, car_details WHERE cars_for_sale.CarId = car_details.CarId \
            ORDER BY price DESC, make, model DESC" + str(num_of_highest)
cursor.execute(sql) 
rows = cursor.fetchall()
for row in rows:
    print "%s %s $%d" % (row[0], row[1], row[2]) 
cursor.close()
connection.close()

测试用例

Trying:
top_N_expensive(10)
Expecting:
VOLKSWAGEN GOLF $1300000
MERCEDES-BENZ SL $329990
BMW 3 $299990
MERCEDES-BENZ SL $249990
PORSCHE 911 $249990
MERCEDES-BENZ SL $224990
PORSCHE 911 $219990
PORSCHE 911 $190000
PORSCHE 911 $184990
BMW M5 $180000
**********************************************************************
File "__main__", line 4, in __main__
Failed example:
top_N_expensive(10)
Exception raised:
Traceback (most recent call last):
  File "C:\Program Files\Python27\lib\doctest.py", line 1254, in __run
    compileflags, 1) in test.globs
  File "<doctest __main__[0]>", line 1, in <module>
    top_N_expensive(10)
  File "Z:\Documentstop_N_expensive.py", line 82, in top_N_expensive
    cursor.execute(sql)
  File "C:\Program Files\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "C:\Program Files\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC10' at line 1")
4

4 回答 4

3

与上一个略有不同:

for row in rows:
    print("{} {} ${:0.0f}".format(*row))

编辑:您的编辑的后续行动 - 您在 E55 之前返回 CLS,它希望他们以另一种方式。检查您的排序顺序;尝试

... ORDER BY price DESC, make, model DESC
于 2012-05-22T14:26:00.597 回答
1

如果您想在SELECT语句中执行此操作,可以尝试使用CONCATand ROUND

SELECT CONCAT('$', ROUND(car_details.price)) AS price FROM car_details;
于 2012-05-22T14:27:58.083 回答
0

做一些简单的字符串格式化应该会让你到达那里:

for row in rows:
    print "%s %s $%d" % (row[0], row[1], row[2])

%s字符用于输出字符串。%d用于输出整数(没有小数部分的数字),所以有点作弊。您还可以使用%.0f显示以 0 位小数打印的浮点数(而%.2f将数字显示到小数点后两位)。

于 2012-05-22T14:10:23.940 回答
0

看看这个问题货币格式在 puython,它很可能是你想要的。作为一种更 hacky 的方式,您可以转换为字符串并使用字符串操作。

编辑

从上面引用的问题中复制

>>> import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'$188518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'$188,518,982.18'
于 2012-05-22T14:05:15.187 回答