IT 인터넷/Python
[Python] MySQL 접속
Banjubu
2021. 4. 20. 21:15
반응형
PyMySQL을 설치해요.
pip install PyMySQL
main.py
import sys
import logging
import pymysql
HOST = "수정하세요"
PORT = 3306
USERNAME = "수정하세요"
PASSWORD = "수정하세요"
DATABASE = "수정하세요"
def connect(host, port, username, password, database):
try:
conn = pymysql.connect(host=host, user=username, passwd=password,
db=database, port=port, use_unicode=True, charset='utf8')
cursor = conn.cursor()
return conn, cursor
except:
logging.error("connect failed")
sys.exit(1)
def main():
conn, cursor = connect(HOST, PORT, USERNAME, PASSWORD, DATABASE)
try:
query = """SELECT now()"""
cursor.execute(query)
conn.commit()
result = cursor.fetchall()
for row_data in result:
print(row_data[0])
finally:
conn.close()
if __name__ == "__main__":
main()
실행해서 이런 식으로 나오면 성공이에요.
2021-04-21 12:02:34
영어, 중국어 공부중이신가요?
홈스쿨 교재. 한 권으로 가족 모두 할 수 있어요!
반응형
LIST