Python-MySql 연동하기 with PyMySql

3847 단어 pythonpython

PyMySql DB 연결

import pymysql

conn = pymysql.connect(user='유저명', passwd='패스워드',
                       host='IP', db='데이터베이스명', charset='utf8')
cursor = conn.cursor()

PyMySql 쿼리 실행 (단일행 삽입)

query = "INSERT INTO post (id, name, title, link, published, topic, platform) VALUE (0, %s, %s, %s, %s, %s, %s)"
data = (name, title, link, published, topic, platform)
cursor.execute(query, data)
conn.commit()

PyMySql 쿼리 실행 (다중행 삽입)

query = "INSERT INTO post (id, name, title, link, published, topic, platform) VALUE (0, %s, %s, %s, %s, %s, %s)"
data = [(name, title, link, published, topic, platform), (name2, title2, link2, published2, topic2, platform2)...]
cursor.executemany(query, data)
conn.commit()

PyMySql DB 연결 해제

conn.close()

좋은 웹페이지 즐겨찾기