```import pymysql
# 1. 创建连接
conn = pymysql.connect(
host="127.0.0.1", port=3306, user="root",
db="tedu1", charset="utf8"
)
# 2. 通过连接对象创建游标
cur = conn.cursor()
# 3.1建表
sql = """CREATE TABLE yg(
FIRST_NAME VARCHAr(20),
LAST_NAME VARCHAr(20),
AGE INT,
SEX VARCHAr(1),INCOME FLOAT
)"""
# cur.execute(sql)
# 3.2 insert
# sql = "INSERT INTO yg VALUES (%s, %s, %s, %s, %s);"
# cur.executemany(sql, [
# ('Mac', 'A', 20, 'M', 20000),
# ('Tom', 'B', 20, 'F', 30000),
# ('Bob', 'C', 20, 'M', 40000)
# ]
# )
# 3.3 SELECt
# sql = "SELECT * FROM yg;"
# cur.execute(sql)
# print(cur.fetchone()) # 1
# print(cur.fetchmany(2)) # 2 3
# 3.4 update
# sql = "UPDATE yg SET INCOME=%s WHERe FIRST_NAME=%s;"
# cur.execute(sql, (10000, "Mac"))
# 3.5 delete
sql = "DELETE FROM yg WHERe FIRST_NAME=%s;"
cur.execute(sql, ("Tom",))
# 4.提交事务关闭资源
conn.commit()
cur.close()
conn.close()