增删改
from pymysql import *
def main():
conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')
cs1 = conn.cursor()
count = cs1.execute('insert into goods_cates(name) values("硬盘")')
print(count)
count = cs1.execute('insert into goods_cates(name) values("光盘")')
print(count)
conn.commit()
cs1.close()
conn.close()
if __name__ == '__main__':
main()
查询一行数据
from pymysql import *
def main():
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
cs1 = conn.cursor()
count = cs1.execute('select id,name from goods where id>=4')
print("查询到%d条数据:" % count)
for i in range(count):
result = cs1.fetchone()
print(result)
cs1.close()
conn.close()
if __name__ == '__main__':
main()
查询多行数据
from pymysql import *
def main():
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
cs1 = conn.cursor()
count = cs1.execute('select id,name from goods where id>=4')
print("查询到%d条数据:" % count)
result = cs1.fetchall()
print(result)
cs1.close()
conn.close()
if __name__ == '__main__':
main()