Intro: I have been working on building a Python backed PostgreSQL database of a Swiss style tournament using Vagrant running a Ubuntu VM for my Relational Database course in Udacity.
Question: Why is the user input name causing an integer error?
Vagrant:
vagrant@vagrant:/vagrant/tournament$ python tournament_test.py
1. countPlayers() returns 0 after initial deletePlayers() execution.
Traceback (most recent call last):
File "tournament_test.py", line 151, in <module>
testCount()
File "tournament_test.py", line 26, in testCount
registerPlayer("Chandra Nalaar")
File "/vagrant/tournament/tournament.py", line 50, in registerPlayer
cursor.execute("INSERT INTO players VALUES (%s)", (name,))
psycopg2.DataError: invalid input syntax for integer: "Chandra Nalaar"
LINE 1: INSERT INTO players VALUES ('Chandra Nalaar')
^
SQL:
CREATE TABLE players (player_id SERIAL UNIQUE PRIMARY KEY, player_name
VARCHAR(40));
Python:
def registerPlayer(player_name):
db = connect()
cursor = db.cursor()
cursor.execute("INSERT INTO players VALUES (%s)", (player_name,))
player_id = cursor.fetchone()[0]
db.commit()
cursor.close()
db.close()
Python (user input test):
registerPlayer("Chandra Nalaar")
Update 1: So I altered the execute statement syntax to include the row specification as recommended by PRMoureu and I returned a new error.
Vagrant:
vagrant@vagrant:/vagrant/tournament$ python tournament_test.py
1. countPlayers() returns 0 after initial deletePlayers() execution.
Traceback (most recent call last):
File "tournament_test.py", line 151, in <module>
testCount()
File "tournament_test.py", line 26, in testCount
registerPlayer("Chandra Nalaar")
File "/vagrant/tournament/tournament.py", line 50, in registerPlayer
cursor.execute("INSERT INTO players (player_name) VALUES (%s)",
(player_name,))
psycopg2.ProgrammingError: column "player_name" of relation "players" does
not exist
LINE 1: INSERT INTO players (player_name) VALUES ('Chandra Nalaar')
^
Which lead me to the conclusion that I am not using my current tournament database. So I went back to Vagrant and imported my file with \i which gave me a new error.
Vagrant: psql:tournament.sql:11: ERROR: relation "players" already exists
So I updated my SQL file with DROP IF EXIST commands learned Here from another thread and was able to get through the Register_Player errors.
Thanks PRMoureu