2023-03-06 13:52:34
import mysql.connector
 
# connect to the MySQL server
cnx = mysql.connector.connect(
    host="localhost",
    user="your_username",
    password="your_password",
    database="your_database"
)
 
# create a cursor object
cursor = cnx.cursor()
 
# get column names from the source table
describe_query = "DESCRIBE source_table"
cursor.execute(describe_query)
columns = [column[0] for column in cursor.fetchall()]
 
# select all rows from the source table
select_query = f"SELECT * FROM source_table"
cursor.execute(select_query)
rows = cursor.fetchall()
 
# construct the insert query with dynamic column names
column_names = ",".join(columns)
value_placeholders = ",".join(["%s"] * len(columns))
insert_query = f"INSERT INTO destination_table ({column_names}) VALUES ({value_placeholders})"
 
# insert rows into the destination table
for row in rows:
    cursor.execute(insert_query, row)
 
# commit the changes
cnx.commit()
 
# close the cursor and database connection
cursor.close()
cnx.close()
 
Invalid Email or Password