用Python创建表的PostgreSQL失败了

时间:2018-01-25 07:35:17

标签: python postgresql psycopg2

由于某些未知原因,未创建数据库中的表。怎么解决?

import psycopg2
conn = psycopg2.connect(user='postgres', host='localhost', password='123456')
conn.autocommit = True
cur = conn.cursor()
cur.execute('CREATE DATABASE example;')
cur.execute("""CREATE TABLE HARMKA
                         (ID INT PRIMARY KEY NOT NULL,
                         PHARMACY_LICENSE CHAR(100),
                         STREET CHAR(150),
                         CITY CHAR(30));""")
cur.execute("INSERT INTO HARMKA VALUES(%s, %s, %s, %s)", (1, '12345', 'street', 'Denwer'))
cur.close()
conn.close()

1 个答案:

答案 0 :(得分:3)

您需要连接到example数据库,然后创建表并操作数据。

conn = psycopg2.connect(database='example', user='postgres', host='localhost', password='123456')