Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 34 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
# MySQL CSV Importer
# CSV to MySQL

This script imports data from a CSV file into a MySQL database. It connects to a MySQL server, checks if the specified database exists (and creates it if it doesn't), then creates a table and imports data from the CSV file into that table.
[![Build Status](https://img.shields.io/github/actions/workflow/status/COMMANDO2406/CSVtoMySQL/test.yml?branch=main)](https://github.com/COMMANDO2406/CSVtoMySQL/actions)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python Version](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/downloads/)

## Prerequisites
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.

1. Python 3.x
2. `mysql-connector-python` library
3. MySQL server
## 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](#installation)
- [Usage](#usage)
- [Configuration](#configuration)
- [Contributing](#contributing)
- [License](#license)

## Installation

1. **Clone the repository:**
### Prerequisites

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

### Steps

1. Clone the repository:

```bash
https://github.com/COMMANDO2406/CSVtoMySQL.git
git clone https://github.com/COMMANDO2406/CSVtoMySQL.git
cd CSVtoMySQL
```

2. **Install the required Python packages:**
2. Install the required Python packages:

```bash
pip install mysql-connector-python
```

3. **Change the `config.py` file:**
3. Create a `config.py` file:

```python
# config.py
Expand All @@ -32,80 +53,18 @@ This script imports data from a CSV file into a MySQL database. It connects to a
password = 'your_mysql_password'
```

4. **Prepare your CSV file:**
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:**
1. Run the script:

```bash
python csv_to_mysql.py
```

2. **Enter the required information when prompted:**
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`):**

```csv
name,age,city
Alice,30,New York
Bob,25,Los Angeles
```

2. **Run the script:**

```bash
python import_csv.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.
```

## How It Works

1. **Connects to the MySQL server:**
The script connects to the MySQL server using the credentials specified in `config.py`.

2. **Checks if the database exists:**
If the database does not exist, it creates the database.

3. **Creates the table:**
The script creates a table with the specified name and columns if it doesn't already exist.

4. **Imports data from the CSV file:**
The script reads the CSV file and inserts the data into the table.

## Main Logic

![Main Logic](imgs/code.png)

## Notes

- The column names you enter should match the columns in your CSV file.
- The script assumes that all columns in the CSV file are of type `VARCHAR(255)`. Modify the script if you need different data types.
- Make sure the `main.csv` file is in the same directory as the script or provide the correct path.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
10 changes: 10 additions & 0 deletions my-docs/docs/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Configuration

Create/update the `config.py` file with the following content:

```python
# config.py
host = 'your_mysql_host'
user = 'your_mysql_user'
password = 'your_mysql_password'
```
42 changes: 42 additions & 0 deletions my-docs/docs/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Contributing

We welcome contributions! Here’s how you can help:

1. **Fork the repository**
2. **Create a new branch** (e.g. `feature/my-new-feature`)
3. **Commit your changes**
4. **Push to the branch**
5. **Create a Pull Request**

Please ensure your code follows our coding standards and includes appropriate tests.

## Structure of this project

```
my-docs/
├── docs/
│ ├── introduction.md
│ ├── installation.md
│ ├── usage.md
│ ├── config.md
│ ├── contributing.md
│ └── license.md
├── index.html
└── style.css
imgs/
└── code.png
config.py
csv_to_mysql.py
LICENSE
main.csv
README.md
```

## Long To-Do's:

Please contact the repository owner if you would like to contribute or check for any branches with similar names

1. Data Validation
2. API Integration
3. Logging
4. GUI setup
34 changes: 34 additions & 0 deletions my-docs/docs/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Installation

## Prerequisites

1. Python 3.x
2. `mysql-connector-python` library
3. MySQL server

## Installation Steps

1. **Clone the repository:**

```bash
git clone https://github.com/COMMANDO2406/CSVtoMySQL.git
cd CSVtoMySQL
```

2. **Install the required Python packages:**

```bash
pip install mysql-connector-python
```

3. **Create a `config.py` file:**

```python
# 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.
5 changes: 5 additions & 0 deletions my-docs/docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

Welcome to the CSV to MySQL project documentation.

This project allows you to import data from a CSV file into a MySQL database. It connects to a MySQL server, checks if the specified database exists (and creates it if it doesn't), then creates a table and imports data from the CSV file into that table.
25 changes: 25 additions & 0 deletions my-docs/docs/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# License

This project is licensed under the MIT License.

MIT License

Copyright (c) 2023 Raghav Sethi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions my-docs/docs/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Usage

1. **Run the script:**

```bash
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`):**

```csv
name,age,city
Alice,30,New York
Bob,25,Los Angeles
```

2. **Run the script:**

```bash
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.
```
43 changes: 43 additions & 0 deletions my-docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>CSV to MySQL Docs</title>
<script
type="module"
src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@2/dist/zero-md.min.js"
></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header>
<h1>CSV to MySQL</h1>
</header>
<div class="container">
<nav>
<ul>
<li><a href="#" data-doc="introduction.md">Introduction</a></li>
<li><a href="#" data-doc="installation.md">Installation</a></li>
<li><a href="#" data-doc="config.md">Configuration</a></li>
<li><a href="#" data-doc="usage.md">Usage</a></li>
<li><a href="#" data-doc="contributing.md">Contributing</a></li>
<li><a href="#" data-doc="license.md">License</a></li>
</ul>
</nav>
<main>
<zero-md src="docs/introduction.md"></zero-md>
</main>
</div>
<script>
document.querySelectorAll("nav a").forEach((link) => {
link.addEventListener("click", function (event) {
event.preventDefault();
const doc = this.getAttribute("data-doc");
document.querySelector("zero-md").src = `docs/${doc}`;
});
});
</script>
</body>
</html>
Loading