In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table.
To connect to MySQL database using python, we need PyMySql module. The cursor class allows python to execute SQL commands. Cursors are created by connection_name.cursor() method, where connection_name is the link made to SQL Database. Once the connection is established, cursor.execute() is used to run the SQL statements.
Let us understand the above by taking an example. Suppose, in MySQL we create a database test and it contains a table named neveropen and has the below schema and following data:
To Copy a Table in MySQL we use the below query:
CREATE TABLE table-name SELECT * FROM table-name;
Now, below is the program to copy the entire table using 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 neveropencopy and copy all # records from neveropen into the newly created table mycursor.execute( "create table neveropencopy select * from neveropen" ) # 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 neveropencopy" ) # fetchall() will store all the records # of copy table into query2 query2 = mycursor.fetchall() # print all records for i in query2: print (i) |
Output:
In the above figure, we could see the list of tables followed by all records from the neveropencopy table. The above output has also been confirmed by providing the output of MySQL database.
Here is another example which depicts how to create a new table from the data and schema of a previous table. Below is the previously existing table:
Now, using the below script to create a copy of the above table in the database:
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 Lazyroardemocopy select * from Lazyroardemo" ) # 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 Lazyroardemocopy" ) # fetchall() will store all the records of copy table into query2 query2 = mycursor.fetchall() # print all records for i in query2: print (i) |
Output:
Below is the new table whose data and schema are copied from the previous table: