python mysql简单的增删改查

import pymysql

#打开数据库连接
db=pymysql.Connect(host="*",port=3306,user="*",password="123456",db="mysqltest",charset="utf8")
#创建游标对象
cursor=db.cursor()
#需要执行的SQL语句
# sql="""create table student(
#     id int not null,
#     name varchar(20),
#     age int
# )"""
#创建表
# sql="""insert into student(id,name,age)
#         values(1,"zhangsan",12)
# """
#查询
#sql="select * from student"
#更新
#sql="update student set age=age+1"
#删除
sql="delete from student where name='zhangsan'"
#执行语句
cursor.execute(sql)
#查询一个
#data1=cursor.fetchone()
##查询所有
#data2=cursor.fetchall()
#提交语句
db.commit()

db.close()
#print(data2)
#print(type(data2))


猜你喜欢

转载自blog.csdn.net/weixin_44371918/article/details/90039085