[Python] After postgresql inserts data, conn and cursor submit and close problems
When using psycopg2 to insert data into the database, you need to conn.cursor()
create , and then use the cursor object to execute SQL insert statements. After inserting data, you can use conn.commit()
commit transaction to save the insert operation to the database.
Before committing the transaction, you may choose to cur.close()
close , but this is not required. If you close the cursor object before committing the transaction, but try to use it after committing the transaction, you will get an error.
The following is a sample code for inserting data into a PostgreSQL database, which demonstrates the process of committing a transaction after inserting data:
import psycopg2
# 连接数据库
conn = psycopg2.connect(
host="your_host_name",
database="your_database_name",
user="your_username",
password="your_password"
)
# 创建游标对象
cur = conn.cursor()
# 插入数据
cur.execute("INSERT INTO your_table_name (column1, column2, column3) VALUES (%s, %s, %s)", (value1, value2, value3))
# 提交事务
conn.commit()
# 关闭游标和连接
cur.close()
conn.close()
In the above code, we psycopg2
first connect
connect to the PostgreSQL database using the function of the module. Then, we created a cursor object cur
and used execute
the method to execute a SQL insert statement to insert the data into the database. Finally, we commit the transaction using commit
the method , saving the insert operation to the database. Note that if you do not call commit
the method , the data will not be saved to the database.
Finally, we close the cursor object and database connection using close
the method . Note that once a connection is closed, you will no longer be able to do anything with that connection object.
If you don't want to use commit
the method to manually commit transactions, you can conn.autocommit
set the property to True
, which will make each SQL statement automatically committed to the database without manually calling commit
the method .
The following is an example showing how to set autocommit
the property to True
and use execute
the method to insert data into a PostgreSQL database:
import psycopg2
# 连接数据库,并设置 autocommit 属性为 True
conn = psycopg2.connect(
host="your_host_name",
database="your_database_name",
user="your_username",
password="your_password",
autocommit=True
)
# 创建游标对象
cur = conn.cursor()
# 插入数据
cur.execute("INSERT INTO your_table_name (column1, column2, column3) VALUES (%s, %s, %s)", (value1, value2, value3))
# 关闭游标和连接
cur.close()
conn.close()
In the code above, we use psycopg2
the module 's connect
function to connect to the PostgreSQL database and set autocommit
the property to True
. Then, we created a cursor object cur
and used execute
the method to execute a SQL insert statement to insert the data into the database. Since we have set autocommit
the property to True
, each SQL statement is automatically committed to the database without manually calling commit
the method .
Finally, we close the cursor object and database connection using close
the method . Note that once a connection is closed, you will no longer be able to do anything with that connection object.