데이터베이스에서 데이터에 액세스하고 파이썬에서 데이터를 조작하는 작업이 포함됩니다. 데이터베이스 이름이 inb104, 그것은 두 개의 테이블, car_details 및Python MySQL 데이터 문자열 조작
cars_for_sale를 포함하는 것은 여기에 지금까지
def top_N_models(carbrand, num_of_highest):
connection = MySQLdb.connect(host='localhost', user='root', \
passwd='root', db='inb104')
cursor = connection.cursor()
sql = str("SELECT model, COUNT(model) AS Count FROM \
cars_for_sale WHERE make='"+carbrand+"' GROUP BY \
model ORDER BY count DESC, model LIMIT "+str(num_of_highest))
cursor.execute(sql)
rows = cursor.fetchall()
try:
test = rows[0][0]
except:
print "No such make of car!"
for row in rows:
print str(row[0]), str(row[1])
cursor.close()
connection.close()
이 코드는 테스트 케이스의 시리즈를 통해 실행 이제 때 여기에 결과가 내 코드입니다. 실패한 테스트 케이스에서 "No such make of car!"대신에 아무것도 반환하지 않기를 기대하고 있습니다. SQL 문이 LIMIT 0
을 가지고 있기 때문에
Trying:
top_N_models('BMW', 6)
Expecting:
3 152
X5 72
5 39
1 19
7 9
M3 9
ok
Trying:
top_N_models('TOYOTA', 10)
Expecting:
COROLLA 275
LANDCRUISER 193
RAV4 189
HILUX 179
CAMRY 137
ECHO 69
KLUGER 61
YARIS 56
AURION 22
TARAGO 22
ok
Trying:
top_N_models('HOLDEN', 1)
Expecting:
COMMODORE 426
ok
Trying:
top_N_models('EDSEL', 10)
Expecting:
No such make of car!
ok
Trying:
top_N_models('TOYOTA', 0)
Expecting nothing
**********************************************************************
File "__main__", line 34, in __main__
Failed example:
top_N_models('TOYOTA', 0)
Expected nothing
Got:
No such make of car!
Trying:
top_N_models('MINI', 1)
Expecting:
COOPER 34
ok
Trying:
top_N_models('HSV', 10)
Expecting:
CLUBSPORT 21
COUPE 5
MALOO 5
AVALANCHE 2
GTS 2
GRANGE 1
ok
1 items had no tests:
__main__.top_N_models
**********************************************************************
1 items had failures:
1 of 7 in __main__
7 tests in 2 items.
6 passed and 1 failed.
***Test Failed*** 1 failures.
누구든지 도움을받을 수 있습니까? – user183651
누군가 (또는 자신이) 문제를 해결하는 경우 대답을 수락으로 표시하고 질문을 삭제하지 마십시오. 혼자서 해결하면 일정한 지연 후에 자신의 질문에 대답 할 수 있습니다. –