简介: 我一直在努力构建一个由Python支持的PostgreSQL数据库,该数据库使用Vagrant运行一个ubuntuvm,用于我在Udacity的关系数据库课程。在
问题: 为什么用户输入名称会导致整数错误?在
流浪汉:
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语言:
^{pr2}$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(用户输入测试):
registerPlayer("Chandra Nalaar")
更新1: 因此,我修改了execute语句语法,使之包括PRMoureu建议的行规范,并返回了一个新错误。在
流浪汉:
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')
^
这让我得出结论,我没有使用我目前的锦标赛数据库。所以我回到Vagrant并用\I导入了我的文件,这给了我一个新的错误。在
流浪汉: psql:tournament.sql:11:错误:关系“players”已存在
所以我用dropif-EXIST命令更新了我的SQL文件,从另一个线程中学习了Here,并能够解决Register\u播放器错误。在
谢谢PRMoureu
在您使用的
INSERT
语句中,它要求所有字段都有一个值,第一个是player_id
(player_id
是一个整数)。在您需要指定一个ID:
或者将语法改为:
^{pr2}$相关问题 更多 >
编程相关推荐