꿈을 향해 on my way

Connect MySQL database with Python 본문

데이터 사이언스 공부

Connect MySQL database with Python

박재성 2021. 12. 22. 04:33

1. Import libraries

import mysql.connector
import pandas as pd

If you don’t have mysql.connector, go here / if you don’t have pandas, go here

2. Create a connection

cnt = mysql.connector.connect(
    host="datawarehouse.brightpowerinc.com",
    user= "jpark", # your dbeaver id
    passwd= "legendaryfob" # your dbeaver password
    )

*Credentials (e.g. DBeaver username and password) are sensitive information and should be stored securely. Managing credentials in environment variable is highly recommended. If you are not familiar with environmental variable, Using .env Files for Environment Variables in Python Applications.

3. Write a query

example_query = (
    '''
    SELECT *
    FROM babl_expansion.cube_bill cb
    LIMIT 5
    '''
    )

4. Read the query with pandas dataframe

df = pd.read_sql_query(example_query, cnt)
beautiful
beautiful
Comments