import pymysql
#创建连接
conn=pymysql.connect(host="localhost",port=3306,user="root",passwd="123456",db="python_test")
#创建游标
cursor=conn.cursor()
#..............操作过程
#关闭游标
cursor.close()
#关闭连接
conn.close()
import pymysql
conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",db="it",charset="utf8")
cursor=conn.cursor()
sql="""
create table user(
id int PRIMARY KEY auto_increment,
username VARCHAR(20),
password VARCHAR(20),
address VARCHAR(35)
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
import pymysql
#创建连接
conn=pymysql.connect(host="localhost",port=3306,user="root",passwd="123456",db="python_test",charset="utf8")
#创建游标
cursor=conn.cursor()
cursor.execute("select * from student;")
print(cursor.fetchone())#获取一条
print(cursor.fetchmany(2))#获取两条
print(cursor.fetchall())#获取结果集合中的全部
#关闭游标
cursor.close()
#关闭连接
conn.close()
import pymysql
#创建连接
conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",db="python_test",charset="utf8")
#创建游标
cursor=conn.cursor()
print("-----------插入----------------")
cursor.execute("insert into student values ('nazha',2000,'男');")
cursor.execute("select * from student;")
print(cursor.fetchall())
print("-----------插入----------------")
#cursor.executemany(self,query,args)
cursor.executemany("insert into student value(%s,%s,%s);",[('zhangsan',18,'男'),('lisi',18,'男')])
cursor.execute("select * from student;")
print(cursor.fetchall())
print("-----------修改----------------")
cursor.execute("update student set name = 'zhangsan1' where name = 'zhangsan';")
cursor.execute("select * from student;")
print(cursor.fetchall())
print("----------删除-----------------")
cursor.execute("delete from student where name = 'lisi';")
cursor.execute("select * from student;")
print(cursor.fetchall())
print("---------------------------")
#需要提交才能插入、成功修改、删除
conn.commit()
#关闭游标
cursor.close()
#关闭连接
conn.close()
(('lilei', 18, '男'), ('hanmeimei', 18, '女'), ('huluwa', 18, '男'), ('sunwukong', 18, '男'), ('baigujing', 3000, '女'), ('nazha', 2000, '男'))
---------------------------
(('lilei', 18, '男'), ('hanmeimei', 18, '女'), ('huluwa', 18, '男'), ('sunwukong', 18, '男'), ('baigujing', 3000, '女'), ('nazha', 2000, '男'), ('zhangsan', 18, '男'), ('lisi', 18, '男'))
---------------------------
(('lilei', 18, '男'), ('hanmeimei', 18, '女'), ('huluwa', 18, '男'), ('sunwukong', 18, '男'), ('baigujing', 3000, '女'), ('nazha', 2000, '男'), ('zhangsan1', 18, '男'), ('lisi', 18, '男'))
---------------------------
(('lilei', 18, '男'), ('hanmeimei', 18, '女'), ('huluwa', 18, '男'), ('sunwukong', 18, '男'), ('baigujing', 3000, '女'), ('nazha', 2000, '男'), ('zhangsan1', 18, '男'))
---------------------------
import pymysql
#创建连接
# conn=pymysql.connect(host="localhost",port=3306,user='root',passwd='123456',db='python_test')
conn=pymysql.connect(host="localhost",port=3306,user='root',passwd='123456',db='python_test',charset="utf8")
#创建游标
cursor = conn.cursor()
effect_row= cursor.execute("select * from student;")
print("执行成功,受影响行数:",effect_row)
print(cursor.fetchall())
conn.commit()
cursor.close()
conn.close()