web-dev-qa-db-ja.com

Postgres Psycopg2 Create Table

postgresとPythonは初めてです。単純なユーザーテーブルを作成しようとしていますが、なぜ作成されないのかわかりません。エラーメッセージは表示されませんが、

    #!/usr/bin/python
    import psycopg2

    try:
        conn = psycopg2.connect(database = "projetofinal", user = "postgres", password = "admin", Host = "localhost", port = "5432")
    except:
        print("I am unable to connect to the database") 

    cur = conn.cursor()
    try:
        cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    except:
        print("I can't drop our test database!")

    conn.close()
    cur.close()
5
Ricardo Pinto

データベースへのコミットを忘れています!

import psycopg2

try:
    conn = psycopg2.connect(database = "projetofinal", user = "postgres", password = "admin", Host = "localhost", port = "5432")
except:
    print("I am unable to connect to the database") 

cur = conn.cursor()
try:
    cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
except:
    print("I can't drop our test database!")

conn.commit() # <--- makes sure the change is shown in the database
conn.close()
cur.close()

`

7
Matt