Skip to content

Instantly share code, notes, and snippets.

@ankiyong
Last active October 23, 2021 13:02
Show Gist options
  • Save ankiyong/363396f88f109d5660926f7199540796 to your computer and use it in GitHub Desktop.
Save ankiyong/363396f88f109d5660926f7199540796 to your computer and use it in GitHub Desktop.
python mysql 연동
pip install pymysql
import pymysql
db = pymysql.connect(
user='유저이름',
passwd='비밀번호',
host='127.0.0.1',
db='디비이름',
charset='utf8'
)
#Dict Insert
cursor = job_db.cursor(pymysql.cursors.DictCursor)
insert_data = [dict형자료] ex) [{'a':1,'b':2,'c':3,'d':4},{'a':1,'b':2,'c':3,'d':4}]
insert_sql = "INSERT INTO `테이블명` VALUES (%(a)s, %(b)s, %(c)s, %(d)s);"
cursor.executemany(insert_sql2, insert_data)
job_db.commit()
insert_data = [list형자료] ex)[['a',1],['b',3]]
insert_sql = "INSERT INTO `테이블명` VALUES (%s, %s);"
cursor.executemany(insert_sql, insert_data)
db.commit()
#조회하기(dataframe형으로 변환하여 확인)
sql = "SELECT * FROM 테이블;"
cursor.execute(sql)
result = cursor.fetchall()
result = pd.DataFrame(result)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment