我调查了其他一些原因并且无法找到答案。该表不是在“”中创建的,并且情况完全相同。我的create table语句如下:
CREATE TABLE players (
name VARCHAR(30),
id SERIAL PRIMARY KEY,
wins INTEGER DEFAULT 0,
matches INTEGER DEFAULT 0
);
我正在运行以下PostgreSQL查询(在Python函数中 - 将一个播放器插入数据库):
query = "INSERT INTO players (name) VALUES (%s);"
接下来是这个:
query = "SELECT id, name, wins, matches\
FROM players ORDER BY wins DESC;"
当我尝试运行脚本时,出现以下错误:
ProgrammingError: column "id" of relation "players" does not exist
我已经尝试取出id列并且只选择胜利,但它告诉我它也不存在。我也尝试将玩家表更改为以下内容:
CREATE TABLE players (
name VARCHAR(30),
id SERIAL PRIMARY KEY,
wins INTEGER,
matches INTEGER
);
INSERT语句为:
query = "INSERT INTO players (name, wins, matches) VALUES (%s,%s,%s);"
c.execute(query,(name,0,0))
并收到以下错误:
ProgrammingError: column "wins" of relation "players" does not exist
我运行了一个简单的SELECT * FROM玩家;在我的终端中,它返回此表,所以列肯定在那里......
name | id | wins | matches
------+----+------+---------
(0 rows)
答案 0 :(得分:0)
你确定在你没有删除表中的任何外键之前导致问题可能是db没有读取任何列,因为它将无法找到对该键的任何引用。 尝试重新设计架构。
答案 1 :(得分:0)
>>> import psycopg2
>>> con = psycopg2.connect(database='game',user='postgres',password='root',
host='localhost',port=5432)
>>> cur = con.cursor()
>>> var = 'name_input'
>>> cur.execute("""INSERT INTO players(name) VALUES ('%s')""" %(var))
>>> win = 1
>>> match = 3
>>> cur.execute("""INSERT INTO scores(wins,matches) VALUES (%d,%d)""" %(win,match))
>>> con.commit()
>>> cur.execute("""INSERT INTO players(name) VALUES ('%s')""" %(var))
>>> con.commit()
>>> cur.execute("SELECT * FROM players")
>>> data = cur.fetchall()
>>> data
[('name_input', 3), ('name_input', 4)]
>>> cur.execute("SELECT * FROM scores")
>>> data = cur.fetchall()
>>> data
[(1, 1, 3)]