본문 바로가기
Computer Languages/Python

Python에서 MySQL 연결하는 방법

by blackcon 2015. 11. 9.
728x90

저는 우분투 14.04에서 수행했는데 pip를 이용하는 것이므로 별 차이는 없을겁니다!

1. pip show MySQL-python // 설치되었는지 확인

2. sudo apt-get install libmysqlclient-dev // 라이브러리 설치

3. sudo apt-get install python-dev // 라이브러리 설치

4 sudo pip install MySQL-python // MySQL-python 설치

5. 설치 확인하기

#!/usr/bin/env python 
import MySQLdb  

host = "localhost" 
db_id = "DBID" 
db_pw = "DB_PWD" 
db_name = "DB_NAME"  
db = MySQLdb.connect( host, db_id, db_pw, db_name ) 

cursor = db.cursor() 
cursor.execute( "select version()" )  
data = cursor.fetchone() 
print data  
db.close() 

참고 : http://duksoo.tistory.com/entry/Python-Mysql-%EC%97%B0%EB%8F%99

728x90

'Computer Languages > Python' 카테고리의 다른 글

Python에 Matplotlib 설치하는 방법  (0) 2022.06.21
Python에서 zlib 압축 푸는 방법  (2) 2016.01.25
pydbg설치 참고 사이트  (0) 2014.11.29
Python에서 JSON 데이터 예시보기  (0) 2013.01.23
urllib.urlencode  (0) 2013.01.22