import pymysql
#1.建立数据库连接
conn = pymysql.connect(host ="localhost",
user ="root",
passwd ="123456",
port =3306,
db ="python",
charset ="utf8")#2.创建游标对象
cur = conn.cursor()#测试SQL语句补位
name1 =input("请输入姓名:")
cur.execute("insert into stu(name) values(%s)",[name1])#3.通过创建游标 cur 执行 sql语句#**************************************************************************#添加语句
sql_insert ="insert into stu VALUE (3,'张无忌','1701');"
cur.execute(sql_insert)#**************************************************************************#删除语句
sql_delect ="delete from stu where id = 2";
cur.execute(sql_delect)#**************************************************************************#这是查询语句
sql_select ="select * from student where ID = %s;"
cur.execute(sql_select,["88"])
L = cur.fetchall()# L的数据类型为元组类型if L ==():print("hahahahah")print(L[0])print(L[0][1])print(L)print(len(L))#**************************************************************************#这是创建表语句
sql_select ="create table tea(" \
"name varchar(50) primary key," \
"pwd varchar(50)" \
")CHARACTER set utf8;"
cur.execute(sql_select)#**************************************************************************
sql_insert ="insert into tea VALUE ('zddds','123');"
cur.execute(sql_insert)
sql_insert ="insert into student VALUE ('zs','12ddd3','123');"
cur.execute(sql_insert)#**************************************************************************#4.提交到数据库进行持久化保存
conn.commit()#5.关闭游标
cur.close()#6断开连接
conn.close()