0
我想寫一個程序來查詢數據庫。數據庫是golfDB,它由一個名爲玩家的表組成,其中包含5個字段:名稱(玩家名稱),totalGross(每輪總分數的總和),totalRounds(玩過的回合數),pars(製作的總數) ,小鳥(小鳥總數),我的程序需要輸出最多的球員,一個輸入球員的平均得分(totalGross/totalRounds),並按照總得分,從最低到最高排序。現在我還沒有真正用過大多數語法分析功能或者函數來計算分數,我的平均分數函數有問題,我收到了這個錯誤,我真的不確定如何修復:查詢一個sqlite3數據庫
Traceback (most recent call last):
File "/Users/tinydancer9454/Documents/python/golfDBuserInterface.py", line 46, in <module>
main()
File "/Users/tinydancer9454/Documents/python/golfDBuserInterface.py", line 40, in main
queryDBavgScore(cursor)
File "/Users/tinydancer9454/Documents/python/golfDBuserInterface.py", line 29, in queryDBavgScore
answer = totalGrossScore/ totalRoundsScore
TypeError: unsupported operand type(s) for /: 'tuple' and 'tuple'
這是我到目前爲止的代碼:
import sqlite3
def getDBCursor(DB):
"""obtain and return a cursor for the database DB"""
conn= sqlite3.connect('/Users/tinydancer9454/Documents/python/golfDB')
cursor= conn.cursor()
return cursor
def queryDBpars(cursor):
"""find out which player had the most pars"""
cursor.execute('select name from players where pars >= 0')
def queryDBavgScore(cursor):
"""find the average score of inputed player"""
player= input("Please enter the player's name: ")
cursor.execute('select totalGross from players where name = ?', (player,))
totalGrossScore = cursor.fetchone()
cursor.execute('select totalRounds from players where name = ?', (player,))
totalRoundsScore = cursor.fetchone()
answer = totalGrossScore/ totalRoundsScore
print('The average score for', player, 'is', answer)
def queryDBplayers(cursor):
"""lists the players in order of their total gross score"""
def main():
"""obtain cursor and query the database: golfDB"""
cursor= getDBCursor('golfDB')
queryDBpars(cursor)
queryDBavgScore(cursor)
queryDBplayers(cursor)
cursor.close()
是的,這是正確的。因爲即使您只取一行,它仍可能包含多個數據字段;例如編號,名稱等 – eandersson 2013-04-24 14:17:12
好的,這使得更多的意義。謝謝。 – tinydancer9454 2013-04-24 14:19:48
不用擔心@ tinydancer9454。很高興我能幫上忙。 – eandersson 2013-04-24 14:25:25