Skip to content

This Python script allows you to import data from a CSV file into a MySQL database. It provides a user-friendly interface to input the database name, table name, and column names for the table. The script dynamically creates the table in the MySQL database with the specified columns and their data types.

License

Notifications You must be signed in to change notification settings

COMMANDO2406/CSVtoMySQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSV to MySQL

Build Status License: MIT Python Version

CSV to MySQL is a simple Python utility that allows you to import data from a CSV file into a MySQL database. This tool checks if the specified database exists and creates it if it doesn't, then creates a table and imports the data from the CSV file.

Features

  • Connect to MySQL database
  • Check and create database if it doesn't exist
  • Create table based on CSV header
  • Import CSV data into MySQL table

Table of Contents

Installation

Prerequisites

  • Python 3.x
  • MySQL server
  • mysql-connector-python library

Steps

  1. Clone the repository:

    git clone https://github.com/COMMANDO2406/CSVtoMySQL.git
    cd CSVtoMySQL
  2. Install the required Python packages:

    pip install mysql-connector-python
  3. Create a config.py file:

    # config.py
    host = 'your_mysql_host'
    user = 'your_mysql_user'
    password = 'your_mysql_password'
  4. Prepare your CSV file: Ensure you have a CSV file named main.csv in the same directory as the script. The CSV file should have a header row with column names matching the ones you will specify in the script.

Usage

  1. Run the script:

    python csv_to_mysql.py
  2. Enter the required information when prompted:

    • Database name: Enter the name of the database you want to use. If it doesn't exist, it will be created.
    • Table name: Enter the name of the table where the data will be imported.
    • Column names: Enter the column names separated by commas. These should match the columns in your CSV file.

Example

Here is an example of how to use the script:

  1. Prepare your CSV file (main.csv):

    name,age,city
    Alice,30,New York
    Bob,25,Los Angeles
    
  2. Run the script:

    python csv_to_mysql.py
  3. Enter the following when prompted:

    Enter database name: my_database
    Enter table name: people
    Enter column names (Separated by commas): name,age,city
    
  4. The script will output:

    Connected successfully
    Database 'my_database' created successfully
    Data imported successfully.
    

About

This Python script allows you to import data from a CSV file into a MySQL database. It provides a user-friendly interface to input the database name, table name, and column names for the table. The script dynamically creates the table in the MySQL database with the specified columns and their data types.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages