查看下面的命令,使用 Python 在 PSQL 中创建表。在 connection.close() 之后是否有任何命令可以打开连接或再次获取与数据库的连接,这样我们就不需要编写整个命令来获取连接并且可以轻松地更改创建的表中的数据??
import psycopg2
try:
connection = psycopg2.connect(database = "staff", user = "XYZ", password = "python", host = "localhost", port = "5432")
except psycopg2.Error as err:
print("An error was generated!")
else:
print("Connection to database was successful!")
cursor = connection.cursor()
cursor.execute('''create table mystaff.employees
(id int primary key not null,
first_name varchar(25) not null,
last_name varchar(25) not null,
department varchar(25) not null,
phone varchar(25),
address varchar(50),
salary int);''')
connection.commit()
connection.close()