如何使用Python批量插入数据到数据库?

  • Post category:Python

以下是如何使用Python批量插入数据到数据库的完整使用攻略。

使用Python批量插入数据到数据库的前提条件

在使用Python批量插入数据到数据库之前,需要确保已经安装并启动了支持批量插入的数据库,例如MySQL或PostgreSQL,并且需要安装Python的相应数据库驱动程序,例如mysql-connector-pythonpsycopg2

步骤1:导入模块

在Python中使用相应的数据库驱动程序连接数据库。以下是导入mysql-connector-python模块的基本语法:

import mysql.connector

以下是导入psycopg2模块的基本语法:

import psycopg2

步骤2:连接数据库

在Python中,可以使用相应的数据库驱动程序连接数据库。以下是连接MySQL数据库的基本语法:

mydb = mysql.connector.connect(
  host="localhost",
  user="your",
  password="yourpassword",
  database="mydatabase"
)

以下是连接PostgreSQL数据库的基本语法:

mydb = psycopg2.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

在上面的语法中,localhost是数据库服务器的主机名,yourusernameyourpassword是数据库的和密码,mydatabase`是要使用的数据库的名称。

步骤3:批量插入数据

在Python中,可以使用executemany()方法批量插入数据。以下是批量插入数据的基本语法:

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
  ('Peter', 'Lowstreet 4'),
  ('Amy', 'Apple st 652'),
  ('Hannah', 'Mountain 21'),
  ('Michael', 'Valley 345'),
  ('Sandy', 'Ocean blvd 2'),
  ('Betty', 'Green Grass 1'),
  ('Richard', 'Sky st 331'),
  ('Susan', 'One way 98'),
  ('Vicky', 'Yellow Garden 2'),
  ('Ben', 'Park Lane 38'),
  ('William', 'Central st 954'),
  ('Chuck', 'Main Road 989'),
  ('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

在上面的语法中,sql是要执行的SQL语句,val是要插入的数据列表。使用executemany()方法批量插入数据。最后,使用commit()方法提交事务,并使用print()函数印插入记录的数量。

示例1

在这个示例中,我们使用Python连接到MySQL数据库,并批量插入数据到customers表中。

以下是Python代码:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
  ('Peter', 'Lowstreet 4'),
  ('Amy', 'Apple st 652'),
  ('Hannah', 'Mountain 21'),
  ('Michael', 'Valley 345'),
  ('Sandy', 'Ocean blvd 2'),
  ('Betty', 'Green Grass 1'),
  ('Richard', 'Sky st 331'),
  ('Susan', 'One way 98'),
  ('Vicky', 'Yellow Garden 2'),
  ('Ben', 'Park Lane 38'),
  ('William', 'Central st 954'),
  ('Chuck', 'Main Road 989'),
  ('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

在上面的代码中,我们首先使用mysql-connector-python模块连接到MySQL数据库。然后,使用executemany()方法批量插入数据到customers表中。最后,使用commit()方法提交事务,并使用print()函数打印插入记录的数量。

示例2

在这个示例中,我们使用Python连接到PostgreSQL数据库,并批量插入数据到customers表中。

以下是Python代码:

import psycopg2

mydb = psycopg2.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
  ('Peter', 'Lowstreet 4'),
  ('Amy', 'Apple st 652'),
  ('Hannah', 'Mountain 21'),
  ('Michael', 'Valley 345'),
  ('Sandy', 'Ocean blvd 2'),
  ('Betty', 'Green Grass 1'),
  ('Richard', 'Sky st 331'),
  ('Susan', 'One way 98'),
  ('Vicky', 'Yellow Garden 2'),
  ('Ben', 'Park Lane 38'),
  ('William', 'Central st 954'),
  ('Chuck', 'Main Road 989'),
  ('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

在上面的代码中,我们首先使用psycopg2模块连接到PostgreSQL数据库。然后,使用executemany()方法批量插入数据到customers表中。最后,使用commit()方法提交事务,并使用print()函数打印插入记录的数量。

以上是如何使用Python批量插入数据到数据库的完整使用攻略,包括导入模块、连接数据库、批量插入数据等步骤。提供了两个示例以便更好地理解如何在Python中批量插入数据到数据库。