📜  如何使用Python在 MySQL 中复制表?

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

如何使用Python在 MySQL 中复制表?

在本文中,我们将在 MySQL 中创建一个表,并将使用Python创建该表的副本。我们将复制整个表,包括所有列和列的定义,以及表中所有行的数据。

要使用Python连接到 MySQL 数据库,我们需要PyMySql模块。游标类允许Python执行 SQL 命令。游标由connection_name.cursor()方法创建,其中connection_name是指向 SQL 数据库的链接。建立连接后,cursor.execute() 用于运行 SQL 语句。

让我们通过一个例子来理解上面的内容。假设,在 MySQL 中,我们创建了一个数据库测试,它包含一个名为geeksfoegeeks的表,并具有以下架构和以下数据:

SQL 数据库

要在 MySQL 中复制表,我们使用以下查询:

CREATE TABLE table-name SELECT * FROM table-name;

现在,下面是使用Python复制整个表的程序:

Python3
# import required modules
import pymysql
  
  
# establish connection to SQL database
connection = pymysql.connect(
    
    # specify hostname
    host="localhost",
      
    # specify user of mysql database
    user="root",
      
    # specify password for above user
    password="1234",
      
    # default port number for mysql is 3306
    port=3306,
      
    # specify database name on which you want to work
    db="test"
)
  
  
# make a cursor
mycursor = connection.cursor()
  
# create a new table geeksforgeekscopy and copy all 
# records from geeksfoegeeks into the newly created table
mycursor.execute("create table geeksforgeekscopy select * from geeksfoegeeks")
  
# list all the tables
mycursor.execute("Show tables")
  
# fetchall() will store all the names 
# of tables into query1
query1 = mycursor.fetchall()
  
# print name of tables
for i in query1:
    print(i)
  
# read all records from copy table
mycursor.execute("Select * from geeksforgeekscopy")
  
# fetchall() will store all the records 
# of copy table into query2
query2 = mycursor.fetchall()
  
# print all records
for i in query2:
    print(i)


Python3
# import required modules
import pymysql
  
# connect python with mysql with your hostname, 
# username, password and database
connection= pymysql.connect("localhost", "root", "", "geek")
  
# make a cursor
mycursor = connection.cursor()
  
# create a new table and copy all records from 
# previous table into the newly created table
mycursor.execute("create table geeksdemocopy select * from geeksdemo")
  
# list all the tables
mycursor.execute("Show tables")
  
# fetchall() will store all the names of tables into query1
query1 = mycursor.fetchall()
  
# print name of tables
for i in query1:
    print(i)
  
# read all records from copy table
mycursor.execute("Select * from geeksdemocopy")
  
# fetchall() will store all the records of copy table into query2
query2 = mycursor.fetchall()
  
# print all records
for i in query2:
    print(i)


输出:

Python输出

在上图中,我们可以看到表列表以及geeksforgeekscopy表中的所有记录。通过提供MySQL数据库的输出也证实了上述输出。

MySQL 输出

这是另一个示例,它描述了如何根据前一个表的数据和架构创建新表。下面是以前存在的表:

现在,使用以下脚本在数据库中创建上表的副本:

蟒蛇3

# import required modules
import pymysql
  
# connect python with mysql with your hostname, 
# username, password and database
connection= pymysql.connect("localhost", "root", "", "geek")
  
# make a cursor
mycursor = connection.cursor()
  
# create a new table and copy all records from 
# previous table into the newly created table
mycursor.execute("create table geeksdemocopy select * from geeksdemo")
  
# list all the tables
mycursor.execute("Show tables")
  
# fetchall() will store all the names of tables into query1
query1 = mycursor.fetchall()
  
# print name of tables
for i in query1:
    print(i)
  
# read all records from copy table
mycursor.execute("Select * from geeksdemocopy")
  
# fetchall() will store all the records of copy table into query2
query2 = mycursor.fetchall()
  
# print all records
for i in query2:
    print(i)

输出:

下面是新表,其数据和架构是从上一个表中复制而来的: