增加
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='python3',user='root',passwd='123456',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("insert into students(sname) values('张飞')")
print(count)
conn.commit()
cs1.close()
conn.close()
print('成功')
except Exception as e:
print ('失败')
修改
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='python3',user='root',passwd='123456',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("update students set sname = '刘邦' where id = 6")
print(count)
conn.commit()
cs1.close()
conn.close()
print('成功')
except Exception as e:
print ('失败')
删除
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='python3',user='root',passwd='123456',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("delete from students where id = 6")
print(count)
conn.commit()
cs1.close()
conn.close()
print('成功')
except Exception as e:
print ('失败')
查
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='python3',user='root',passwd='123456',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("select * from students where id = 7")
#count = cs1.execute("select * from students")
result = cs1.fetchone()#查询一行
#result = cs1.fetchall()#查询多行
print(result)
print(count)
conn.commit()
cs1.close()
conn.close()
print('成功')
except Exception as e:
print ('失败')