2012-05-22 81 views
1

我有一個單一的任務,我跑進了一堵牆,我似乎無法得到它的工作沒有任何我試圖。Python MySQL的數據操作

所以,這項任務主要集中在從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") 
+0

至於零,看起來你需要通過Python浮動功能運行值。至於沒有$,你可能需要打印。 – octopusgrabbus

+0

您是否需要使用sql語句本身來獲取結果,或者您可以自己格式化輸出。如果它晚些時候你已經有了解決方案。 – specialscope

+0

我可以對輸出進行格式化,我猜,無論哪種方式都行得通,這兩種方法都沒有學術限制,只是不知道該如何去做。對不起,我是Python編程新手。 – user183651

回答

0

看看這個問題currency formatting in puython,它是最有可能你想要什麼。作爲一種更黑客的方式,你可以轉換爲字符串和使用字符串操作。

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

%s字符輸出字符串:

編輯

從上面提到的問題

>>> 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' 
+0

如何在這裏舉個例子? – octopusgrabbus

0

做一些簡單的string formatting應該讓你有複製。 %d是用於輸出整數(數字沒有小數部分),所以這是一個作弊。您還可以使用%.0f來顯示打印有0位小數的浮點數(而%.2f會顯示小數點後兩位)。

+0

爲湯姆助陣乾杯,我已經編輯了原始文章與進一步的測試案例,只是一個似乎失敗,但對我來說,我無法弄清楚爲什麼。 – user183651

2

比上一個微小的變化:

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

編輯:一個後續的編輯 - 你E55纔回到CLS,需要他們的其他方式。檢查你的排序順序;嘗試

... ORDER BY price DESC, make, model DESC 
+0

我嘗試使用你的編輯,它似乎使所有測試案例失敗 – user183651

+0

!好的,意外的...你能展示一個由此產生的失敗嗎?'預期'vs'有'? –

+0

嗨,我編輯了原始帖子在底部 – user183651

1

如果你想這樣做的SELECT聲明,你可以用CONCATROUND嘗試:

SELECT CONCAT('$', ROUND(car_details.price)) AS price FROM car_details;