How to connect MySQL DB in Python?

This recipe helps you connect MySQL DB in Python

Recipe Objective

Have you tried to connect MySQL DB in python ?

So this is the recipe on how we can connect MySQL DB in Python.

Access YOLO OCR Character Recognition Project with Source Code

Step 1 - Importing Libraries

import sqlalchemy as sa import pandas as pd

Here we have improted pandas and sqlalchemy which will be needed.

Step 2 - Connecting to SQL

We have first used iris dataset for this and passed the information of SQL and applied dataset.to_sql to save the file as SQL. dataset = pd.read_csv("iris.data.csv") engine_str = ( "mysql+pymysql://{user}:{password}@{server}/{database}".format( user = "root", password = "root888", server = "localhost", database = "datasciencerecipes")) engine = sa.create_engine(engine_str) conn = engine.connect() # check whether connection is Successful or not if (conn): print("MySQL Connection is Successful ... ... ...") else: print("MySQL Connection is not Successful ... ... ...") dataset.to_sql(name="irisdata", con=engine, schema="datasciencerecipes", if_exists = "replace", chunksize = 1000, index=False) conn.close()

 

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python