我正在尝试参加最后的比赛'通过udacity项目介绍关系数据库课程。这是项目描述的链接:
https://docs.google.com/document/d/16IgOm4XprTaKxAa8w02y028oBECOoB1EI1ReddADEeY/pub?embedded=true
我有一个名为tournament.sql的文件,其中有数据库'锦标赛'还有两张表'匹配'和#'球员'定义如下:
DROP DATABASE IF EXISTS tournament;
CREATE DATABASE tournament;
CREATE TABLE IF NOT EXISTS matches (
id SERIAL PRIMARY KEY,
player1 integer references players (id),
player2 integer references players (id)
);
CREATE TABLE IF NOT EXISTS players (
id SERIAL PRIMARY KEY,
name varchar(40)
);
我还在tournament.py文件中定义了两个函数deleteMatches和deletePlayers:
import psycopg2
def connect():
"""Connect to the PostgreSQL database. Returns a database connection."""
return psycopg2.connect("dbname=tournament")
def deleteMatches():
"""Remove all the match records from the database."""
conn = connect()
c = conn.cursor()
c.execute("TRUNCATE TABLE matches;")
conn.commit()
conn.close()
def deletePlayers():
"""Remove all the player records from the database."""
conn = connect()
c = conn.cursor()
c.execute("TRUNCATE TABLE players;")
conn.commit()
conn.close()
还有一个由整个课程的作者预定/预建的python文件' tournament_test.py',可以执行以检查tournament.py中所有必需的功能是否正常工作/做好自己的工作。该文件' tournament_test.py'在命令行中在虚拟机中执行,在我的情况下会产生以下错误:
vagrant@vagrant-ubuntu-trusty-32:/vagrant/tournament$ python tournament_test.py
Traceback (most recent call last):
File "tournament_test.py", line 151, in
testCount()
File "tournament_test.py", line 17, in testCount
deleteMatches()
File "/vagrant/tournament/tournament.py", line 16, in deleteMatches
c.execute("TRUNCATE TABLE matches;")
psycopg2.ProgrammingError: relation "matches" does not exist
有谁知道我的代码有什么问题?我开始失去耐心了。我花了几个小时试图找出错误,我可以找到任何有用的信息。这门课程非常糟糕,草率和不专业。我无法找到合适的词来表达我的挫败感。
答案 0 :(得分:1)
"你可能已经像我一样自己解决了这个问题,但是如果你还在搜索,或者是其他任何可能遇到这个问题的人。我也参加了这门课程并遇到了这个初学者的问题。
这是用户错误。我以错误的方式连接到vagrant和锦标赛数据库。
登录vagrant后,我在正确的文件夹中访问正确的数据库,但方法错误。
错误:
一旦进入流浪汉,我就以用户流浪者的身份访问psql并导入文件。
\i tournament.sql
然后我连接到数据库。
\c tournament
然后我退出psql运行该文件并获得关系不存在错误。
我还需要再做一步。
FIX:
连接并登录数据库锦标赛。我需要再次导入tournament.sql文件。
这创建了实际数据库中的关系,而不仅仅是流浪者或之前我创建它们的地方。
从Vagrant命令Vagrant ssh开始 #分别运行这些命令 cd / vagrant / tournament /psql
\i tournament.sql
\c tournament
\i tournament
#last check to verify your relations were created
\dt
\d (table or view)
这就是为我做的事情。项目的其余部分很简单。我希望这可以帮助任何人在这里寻找答案。" My q&a