[python] db연동 예제

import pymysql
conn = pymysql.connect( host='mysql주소', user='python_user', port=23306, password='python1q2w3e4r!', db='python_test', charset='utf8mb4')
sql = 'insert into sample(memo) values(%s)'
vals = ('삼빠')
cur = conn.cursor()
cur.execute(sql, vals)
conn.commit()
# conn.close()
# for row in cur:
#     print(row)

sql = 'select * from sample'
cur = conn.cursor( pymysql.cursors.DictCursor )
cur.execute( sql )
conn.commit()

rows = cur.fetchall()
for row in rows:
    print( row['seq'], row['memo'] )
#여러 줄 출력
# for row in cur:
# 		print(row['seq'], row['memo'])

#한 줄 출력
# row = cur.fetchone()
# if row == None:
#     print('검색결과없음')
# else:
#     print(row)
#
#     row = cur.fetchone()
#     if row == None:
#         print('검색결과없음')
#     else:
#         print(row)

Subscribe to X세대 신입사원

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe
774-86-01972 cinnabar.3d@gmail.com