This script automates the process of loading data from a CSV file into a MySQL database and dumping the database into an SQL file for backup or migration. It uses Pandas for data manipulation and SQLAlchemy to interface with MySQL.
- Connects to a MySQL database.
- Automatically creates a database if it doesn't exist.
- Loads data from a CSV file into a specified table in the database.
- Exports the entire database to an SQL file with the necessary
CREATE DATABASE
andUSE
statements for easy restoration.
- Python 3.x
- pandas
- mysql-connector-python
- SQLAlchemy
-
Install the required Python libraries using pip:
pip install pandas sqlalchemy mysql-connector-python
-
Make sure MySQL is installed and accessible from your machine.
- Edit the script and replace the placeholders (
your_username
,your_password
,your_host
,your_port
,your_database
, etc.) with your actual MySQL credentials and file paths. - Run the script to load data from a CSV file into your MySQL database:
python your_script_name.py
- The script will also create an SQL dump file, which can be used to restore the database later.
- The data from the CSV file will be written to a MySQL table.
- The database will be dumped into an SQL file with the necessary
CREATE DATABASE
andUSE
statements.
CREATE DATABASE IF NOT EXISTS my_database;
USE my_database;