📜  将Python列表插入 PostgreSQL 数据库

📅  最后修改于: 2022-05-13 01:55:23.868000             🧑  作者: Mango

将Python列表插入 PostgreSQL 数据库

在本文中,我们将讨论如何使用 pyscopg2 模块将Python列表插入到 PostgreSQL 数据库中。

Psycopg2 是Python编程语言最流行的 PostgreSQL 适配器。 Psycopg2 是一个积极开发的符合 DB API 2.0 的 PostgreSQL 驱动程序。它是为多线程应用程序设计的,并管理自己的连接池。可以使用给定的命令安装此模块:

pip install psycopg2

要插入所有记录,将遍历列表并一一插入值。

句法 :

list = [(),(),.....,()]
for d in list:
    cursor.execute("INSERT into table_name(
    column1,column2, colum3.....) VALUES (%s, %s, %s,.....)", d)

首先将所有需要的库导入工作空间并建立数据库连接。将自动提交设置为 false 并创建一个游标对象。现在,创建要插入表中的数据列表。遍历列表并插入值。提交并关闭连接。



示例:将列表值插入数据库

Python3
# importing psycopg2 module
import psycopg2
  
# establishing the connection
conn = psycopg2.connect(
    database="postgres",
    user='postgres',
    password='password',
    host='localhost',
    port='5432'
)
  
# creating a cursor object
cursor = conn.cursor()
  
# creating table
sql = '''CREATE TABLE employee(
 id  SERIAL NOT NULL,
 name varchar(20) not null,
 state varchar(20) not null
)'''
  
  
# list that contain records to be inserted into table
data = [('Babita', 'Bihar'), ('Anushka', 'Hyderabad'), 
        ('Anamika', 'Banglore'), ('Sanaya', 'Pune'),
        ('Radha', 'Chandigarh')]
  
# inserting record into employee table
for d in data:
    cursor.execute("INSERT into employee(name, state) VALUES (%s, %s)", d)
  
  
print("List has been inserted to employee table successfully...")
  
# Commit your changes in the database
conn.commit()
  
# Closing the connection
conn.close()


Python3
# importing psycopg2 module
import psycopg2
  
# establishing the connection
conn = psycopg2.connect(
    database="postgres",
    user='postgres',
    password='password',
    host='localhost',
    port='5432'
)
  
# creating cursor object
cursor = conn.cursor()
  
# query to sort table by name
sql2 = 'select * from employee;'
# executing query
cursor.execute(sql2)
  
# fetching the result
print(cursor.fetchall())
  
# Commit your changes in the database
conn.commit()
  
# Closing the connection
conn.close()


输出:

示例:检查员工表中是否显示数据。

蟒蛇3

# importing psycopg2 module
import psycopg2
  
# establishing the connection
conn = psycopg2.connect(
    database="postgres",
    user='postgres',
    password='password',
    host='localhost',
    port='5432'
)
  
# creating cursor object
cursor = conn.cursor()
  
# query to sort table by name
sql2 = 'select * from employee;'
# executing query
cursor.execute(sql2)
  
# fetching the result
print(cursor.fetchall())
  
# Commit your changes in the database
conn.commit()
  
# Closing the connection
conn.close()

输出