Hallo Ich versuche, eine harte Zeit, um diese Funktion zu machen, in dem ich muß:eine Liste von Tupeln von Datenbank-Datensatz mit Python und postgresql
"""Returns a list of the players and their win records, sorted by wins.
The first entry in the list should be the player in first place, or a player
tied for first place if there is currently a tie.
Returns:
A list of tuples, each of which contains (id, name, wins, matches):
id: the player's unique id (assigned by the database)
name: the player's full name (as registered)
wins: the number of matches the player has won
matches: the number of matches the player has played
"""
zur Zeit habe ich diese Funktion, um zu versuchen zu lösen, dass
Traceback (most recent call last): File "tournament_test.py", line 152, in testStandingsBeforeMatches() File "tournament_test.py", line 61, in testStandingsBeforeMatches raise ValueError("Each playerStandings row should have four columns.") ValueError: Each playerStandings row should have four columns.
Linie 152 in Turnier:: ich erhalte diesen Fehler msg
def playerStandings():
conn = connect()
c = conn.cursor()
c.execute("SELECT id, name \
FROM players LEFT JOIN matches \
ON players.id = matches.id_winner \
ORDER BY players.id")
result = c.fetchall()
conn.close()
return result
und wenn ich den Code ausführen _test.py ist:
testStandingsBeforeMatches()
und die Leitung 61 ist:
if len(standings[0]) != 4:
raise ValueError("Each playerStandings row should have four columns.")
[(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings
und schließlich Variable "Tabellen" ist ein Aufruf an meine Funktion playerStandings() in Zeile 54
standings = playerStandings()
Diese ist mein SQL-Skript zum Erstellen der Datenbank und der Tabellen:
CREATE DATABASE tournament;
\c tournament;
CREATE TABLE players (id SERIAL, name TEXT, PRIMARY KEY (id));
CREATE TABLE matches (
id_match SERIAL,
id_winner SERIAL REFERENCES players(id),
id_looser SERIAL REFERENCES players(id),
PRIMARY KEY (id_match)
);
was kann ich tun, um das zu lösen? Ich bin wirklich neu mit Python, so verstehe ich es nicht sehr gut
Verbinden Sie sich mit der Datenbank mit demselben Benutzer, mit dem Sie diese Tabellen erstellt haben? Sie scheinen einen Berechtigungsfehler zu haben. Wenn Sie einen Benutzer verbinden, mit dem Sie die Tabellen erstellt haben, wird überprüft, ob dies der Fall ist oder nicht. – John
hi @John Ich bearbeitet den Beitrag mit dem richtigen Fehler Bitte überprüfen Sie es erneut –
Das dict haben 4 Elemente in jedem Tupel, aber das Abfrageergebnis haben nur Tow Spalten jeder Zeile, das ist der Code Raise Fehler. –