import pymysql
import traceback
try:
connect=pymysql.connect(charset="utf8",user="root",password="123456",database="student")
cor=connect.cursor()
sql = "delete from stu where id=207;"
cor.execute(sql)
connect.commit()
print("成功删除数据")
except:
print(traceback.format_exc())
修改数据
import traceback
import pymysql
try:
connect=pymysql.connect(user="root",charset="utf8",password="123456",database="student")
cor=connect.cursor()
#打2个单引号是不对的!
sql="update stu set name=\"啊啊啊\",score=45.02 where id=207;"
cor.execute(sql)
connect.commit()
print("数据更改成功")
except:
print(traceback.format_exc())
查询数据
import traceback
import pymysql
try:
connect=pymysql.connect(user="root",charset="utf8",password="123456",database="student")
cor=connect.cursor()
sql="select * from stu where name like '%杰%';"
cor.execute(sql)
all=cor.fetchall()
for asd in all:
print(asd)
except:
print(traceback.format_exc())