Published: Jan 14, 2020 by Dev-hwon
PyMySQL
- mysql을 python에서 사용할 수 있는 라이브러리
설치
pip install pymysql
PyMySQL 예제
import pymysql
import sys
import logging
host = 'Your mysql endpoint url'
port = 3306
username = 'Your username'
database = 'database name'
password = 'Your password'
try:
conn = pymysql.connect(host, user=username, passwd=password, db=database, port=port, use_unicode=True, charset='utf8')
cursor = conn.cursor()
except:
logging.error('could not connect to RDS')
sys.exit(1)
cursor.execute('SHOW TABLES')
cursor.execute('SELECT * FROM artist_genres')
print(cursor.fetchall())
query = "INSERT INTO artist_genres (artist_id, genre) VALUES ('{}', '{}')".format('2345', 'hip-hop')
cursor.execute(query)
conn.commit()
print('success')
sys.exit(0)