即使提交后也没有发现 relations/tables 使用 psycopg2
No relations/tables found using psycopg2 even after committing
我开始使用 psycopg2 并且对 psql 也有基本的了解。我确实在之前成功的数据库之一中创建了 table。但是,在尝试为新数据库创建 table 时,未创建 table。
没有错误,psycopg2
已正确安装在我的 Windows10 上,我也使用 commit()
使数据库更改持久化。我使用 pgAdmin4 GUI 检查并创建了数据库,但是没有创建 tables。
IDE - VSCode ; Postgres v12.2
代码供参考
import psycopg2
connection = psycopg2.connect('dbname=todoapp')
cursor = connection.cursor()
# drop existing table
# cursor.execute('DROP TABLE IF EXISTS todo;')
cursor.execute('''
CREATE TABLE todo(
id serial PRIMARY KEY,
description varchar NOT NULL
);
''')
cursor.execute('INSERT INTO todo(id, description) VALUES (1, buy 2L milk);')
connection.commit()
connection.close()
cursor.close()
未找到关系
单引号搞砸了。使用双引号解决了这个问题。
将插入查询更改为 "INSERT INTO todo(id, description) VALUES (1, 'Buy Milk');" 。
我开始使用 psycopg2 并且对 psql 也有基本的了解。我确实在之前成功的数据库之一中创建了 table。但是,在尝试为新数据库创建 table 时,未创建 table。
没有错误,psycopg2
已正确安装在我的 Windows10 上,我也使用 commit()
使数据库更改持久化。我使用 pgAdmin4 GUI 检查并创建了数据库,但是没有创建 tables。
IDE - VSCode ; Postgres v12.2
代码供参考
import psycopg2
connection = psycopg2.connect('dbname=todoapp')
cursor = connection.cursor()
# drop existing table
# cursor.execute('DROP TABLE IF EXISTS todo;')
cursor.execute('''
CREATE TABLE todo(
id serial PRIMARY KEY,
description varchar NOT NULL
);
''')
cursor.execute('INSERT INTO todo(id, description) VALUES (1, buy 2L milk);')
connection.commit()
connection.close()
cursor.close()
未找到关系
单引号搞砸了。使用双引号解决了这个问题。 将插入查询更改为 "INSERT INTO todo(id, description) VALUES (1, 'Buy Milk');" 。