簡介:我一直在使用Vagrant爲Udacity中的Relational Database課程運行Ubuntu VM構建一個支持Python的PostgreSQL瑞士風格錦標賽數據庫。Python支持的SQL數據庫返回psycopg2.ProgrammingError:嘗試刪除表中的數據時不存在錯誤?
問題:爲什麼當我試圖刪除現有表內的信息時表不存在?
我做了一個通用搜索研究
psycopg2.ProgrammingError
,但是這些信息主要是針對其他情況,我無法做出邏輯連接,讓我解決這個問題。我還發現另一個堆棧溢出線程接近相同的問題other thread here,但該問題也未解決。所以我創造了這個問題,試圖找到能夠理解我的難題的人,並提供某種暗示或線索讓我朝着正確的方向工作。
我已經將錦標賽數據庫SQL文件導入到Vagrant VM中。這刪除了兩個表(球員和比賽)和數據庫(錦標賽),然後重新創建這個數據庫和這些表。所以我知道表(匹配)存在。然而,這就是我得到...
流浪:(當前流浪漢錯誤)
[email protected]:/vagrant/tournament$ python tournament_test.py
Traceback (most recent call last):
File "tournament_test.py", line 151, in <module>
testCount()
File "tournament_test.py", line 17, in testCount
deleteMatches()
File "/vagrant/tournament/tournament.py", line 18, in deleteMatches
cursor.execute("DELETE FROM matches;")
psycopg2.ProgrammingError: relation "matches" does not exist
LINE 1: DELETE FROM matches;
^
SQL:(所有的tournament.sql)
-- Table definitions for the tournament project.
--
-- Put your SQL 'create table' statements in this file; also 'create view'
-- statements if you choose to use it.
--
-- You can write comments in this file by starting them with two dashes,
like
-- these lines here.
DROP TABLE matches;
DROP TABLE players;
DROP DATABASE tournament;
CREATE DATABASE tournament;
CREATE TABLE players (player_id SERIAL UNIQUE PRIMARY KEY, name
VARCHAR(40));
CREATE TABLE matches (match_id SERIAL UNIQUE PRIMARY KEY,
winner INTEGER REFERENCES players(player_id),
loser INTEGER REFERENCES players (player_id));
的Python:(從最小tournament.py)
#!/usr/bin/env python
#
# tournament.py -- implementation of a Swiss-system tournament
#
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."""
db = connect()
cursor = db.cursor()
cursor.execute("TRUNCATE matches CASCADE;")
db.commit()
db.close()
的Python:(最小自tournament_test.py)這是驗證tournament.py文件函數與tournament.sql數據庫一起工作。
#!/usr/bin/env python
#
# Test cases for tournament.py
from tournament import *
def testCount():
"""
Test for initial player count,
player count after 1 and 2 players registered,
player count after players deleted.
"""
deleteMatches()
deletePlayers()
c = countPlayers()
if c == '0':
raise TypeError(
"countPlayers should return numeric zero, not string '0'.")
if c != 0:
raise ValueError("After deletion, countPlayers should return zero.")
print "1. countPlayers() returns 0 after initial deletePlayers()
execution."
registerPlayer("Chandra Nalaar")
c = countPlayers()
if c != 1:
raise ValueError(
"After one player registers, countPlayers() should be 1. Got
{c}".format(c=c))
print "2. countPlayers() returns 1 after one player is registered."
registerPlayer("Jace Beleren")
c = countPlayers()
if c != 2:
raise ValueError(
"After two players register, countPlayers() should be 2. Got
{c}".format(c=c))
print "3. countPlayers() returns 2 after two players are registered."
deletePlayers()
c = countPlayers()
if c != 0:
raise ValueError(
"After deletion, countPlayers should return zero.")
print "4. countPlayers() returns zero after registered players are
deleted.\n5. Player records successfully deleted."
testCount()
流浪(確認表「匹配」的數據庫中存在):
vagrant=> \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+---------
public | matches | table | vagrant
public | players | table | vagrant
(2 rows)
UPDATE2:澄清評論
我連接到數據庫,在我的Python文件的開頭。
def connect()
"""Connect to the PostgreSQL database. Returns a database
connection."""
return psycopg2.connect("dbname=tournament")
你如何連接到數據庫? –
嗨。這是很多有用的信息。請同時閱讀[mcve]並採取行動。另外,你從Google錯誤消息中學到了什麼,可以避免你的特定表/列/約束/等名字?請在你的問題中編輯澄清,而不是評論。請不要在零碎的情況下追加更新,只是讓您的問題保持最新和獨立。 (以前的版本可通過'編輯'鏈接訪問。) – philipxy
謝謝。 [mcve]是一個小例子,不是最基本的摘錄。 (另外,你EDIT2只是再現文本已經在你的問題。)這意味着,全文件,包括可以在不編輯有人用包含最少的代碼出現該問題安裝相應的軟件(蟒蛇/流浪者/ PostgreSQL系統)運行的腳本。不摘錄「來自」這樣的文件。另外,如果在tournament_test.py中調用deleteMatches時執行停止,那麼爲什麼後面的代碼不能執行? PS爲什麼你的流浪者錯誤說「刪除匹配;」當tournament.py說「」TRUNCATE匹配CASCADE;「? – philipxy