[Python] After postgresql inserts data, conn and cursor submit and close problems

[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 psycopg2first connectconnect to the PostgreSQL database using the function of the module. Then, we created a cursor object curand used executethe method to execute a SQL insert statement to insert the data into the database. Finally, we commit the transaction using committhe method , saving the insert operation to the database. Note that if you do not call committhe method , the data will not be saved to the database.

Finally, we close the cursor object and database connection using closethe 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 committhe method to manually commit transactions, you can conn.autocommitset the property to True, which will make each SQL statement automatically committed to the database without manually calling committhe method .

The following is an example showing how to set autocommitthe property to Trueand use executethe 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 psycopg2the module 's connectfunction to connect to the PostgreSQL database and set autocommitthe property to True. Then, we created a cursor object curand used executethe method to execute a SQL insert statement to insert the data into the database. Since we have set autocommitthe property to True, each SQL statement is automatically committed to the database without manually calling committhe method .

Finally, we close the cursor object and database connection using closethe method . Note that once a connection is closed, you will no longer be able to do anything with that connection object.

Guess you like

Origin blog.csdn.net/qq_41604569/article/details/130325769