简体   繁体   中英

Python backed PostgreSQL PSQL user INPUT name confused with invalid integer

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

In the INSERT statement you are using, it expects a value for all fields, with the player_id first ( player_id is an integer).

You need to specify an ID :

cursor.execute("INSERT INTO players VALUES (%s, %s)", (a_new_ID, player_name,))

or change the syntax to this :

cursor.execute("INSERT INTO players (player_name) VALUES (%s)", (player_name,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM