pymysql 라이브러리를 이용해서 mysql에 접근해보자
# in 가상환경
pip install pymysql
# in connection.py
import pymysql
def get_connect():
return pymysql.connect(
host='127.0.0.1',
password='',
user='root',
db='test',
chaeset='utf8'
)
# user 생성까지 한번 해보자
def create_user(name):
conn = get_connection()
curs = conn.cursor()
ok = curs.execute("INSERT INTO users(name) VALUES (%s)", name)
conn.commit()
conn.close()
return json.dumps({'rows': ok})
from connect import *
print(create_user('user'))
{"rows": 1}