PostgreSQL使用Python创建表失败
由于未知原因,数据库中的表未创建。如何解决PostgreSQL使用Python创建表失败,python,postgresql,psycopg2,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
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')
您需要连接到
example
数据库,然后创建表并操作数据
conn = psycopg2.connect(database='example', user='postgres', host='localhost', password='123456')
如果你能告诉我们出了什么问题,你收到了什么错误,或者你所说的“失败”到底是什么意思。。我的意思是那会很漂亮…没有错误。数据库已创建,但表不在数据库中。如果您还提交了事务,该怎么办
conn.commit()
Adelin我认为conn.autocommit=True是相同的,如果你告诉我们出了什么问题,你收到了什么错误,或者你所说的“失败”是什么意思。。我的意思是那会很漂亮…没有错误。数据库已创建,但表不在数据库中。如果您还提交了事务,该怎么办conn.commit()
Adelin我认为conn.autocommit=True是sameimport psycopg2 conn=psycopg2.connect(user='postgres',host='localhost',password='123456')conn.autocommit=True cur=conn.cur()cur.execute('CREATE DATABASE example;')conn=psycopg2.connect(DATABASE='example',user='postgres',host='localhost',password='123456'))conn.autocommit=True cur=conn.cursor()@FootAdministration很高兴它有帮助!导入psycopg2 conn=psycopg2.connect(user='postgres',host='localhost',password='123456')conn.autocommit=True cur=conn.curs()cur.execute('CREATE DATABASE example;')conn=psycopg2.connect(DATABASE='example',user='postgres host='localhost',password='123456 conn.autocommit=True cur=conn.curs())@我很高兴这有帮助!