Psycopg未显示预期数据

时间:2017-03-12 19:16:50

标签: python psycopg2 psycopg

当我按照basic Psycopg example时,我希望从psql中检索数据。

>>> import psycopg2

# Connect to an existing database
>>> conn = psycopg2.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
>>> cur = conn.cursor()

# Execute a command: this creates a new table
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")

# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion (no more SQL injections!)
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
...      (100, "abc'def"))

# Query the database and obtain data as Python objects
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchone()
(1, 100, "abc'def")

# Make the changes to the database persistent
>>> conn.commit()

# Close communication with the database
>>> cur.close()
>>> conn.close()

相反,我看到以下内容:

>>> psql

>>> SELECT * FROM test;

(0 rows)

请指教。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我输入以下内容解决了这个问题:

$ createdb test

$ psql -d test

>>> SELECT * FROM test;