PostgreSQL 使用 Python 创建 table 失败
PostgreSQL creating table with Python failed
由于某些未知原因 table 中的数据库未创建。如何解决?
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()
您需要连接到 example
数据库,然后创建表和操作数据。
conn = psycopg2.connect(database='example', user='postgres', host='localhost', password='123456')
由于某些未知原因 table 中的数据库未创建。如何解决?
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()
您需要连接到 example
数据库,然后创建表和操作数据。
conn = psycopg2.connect(database='example', user='postgres', host='localhost', password='123456')