This Python script allows you to create a MySQL table from a CSV file using the Pandas library. It automatically analyzes the CSV file, determines the data types of each column, and creates a corresponding MySQL table with the appropriate schema. Additionally, it inserts the data from the CSV file into the newly created table.
Before using this script, ensure you have the following installed:
- Python (3.x recommended)
- Pandas library (
pip3 install pandas
) - MySQL Connector (
pip3 install mysql-connector-python
)
-
Clone the repository:
git clone https://github.com/achneerov/easy-python-import-csv-into-mysql.git
-
Navigate to the project directory:
cd easy-import-csv-into-mysql
-
Place your CSV file in the
place-csv-files-here
directory. -
Configure the MySQL connection details in the script:
host='localhost', user='your-username', password='your-password', database='your-database'
-
Run the script:
python insert-into-db.py.py
Modify the following variables in the script according to your needs:
file_name
: The name of your CSV file.table_name
: The desired name for your MySQL table.- MySQL connection details (host, user, password, database).
- The script assumes a local MySQL server. Adjust the
host
variable if your database is hosted elsewhere. - The data types in MySQL are mapped from Pandas data types. You may need to adjust the mapping based on your specific requirements.
Feel free to contribute or report issues. Happy coding!