python操作数据库的基本方法

import  pymysql
ip='localhost'
user='root'
password='000000'
db='wgj01'
port=3306
charset='utf8'
conn=pymysql.connect(host=ip,user=user,password=password,db=db,
                     port=port,charset=charset,autocommit=True)#建立连接
cur=conn.cursor(pymysql.cursors.DictCursor)#游标
sql='select * from students limit 5;'#使用表查询
sql='create table products(id int unique not null,pname varchar(20) not null,counts int not null)'#创建表
sql='insert into products(id,pname,counts)values(22,"huawei7",20),(28,"huawei8",4),(23,"huawei9",6);'#插入多条数据
sql='update products set pname="huawei10" where id=9;'#修改表的数据
sql='delete from products where id=9;'#删除表的数据
cur.execute(sql)#执行sql语句
conn.commit()#提交
all=cur.fetchall()
one=cur.fetchone()
many=cur.fetchmany(2)
cur.close()
conn.close()
print(one)
print(many)
print(all)