pymysql模块连接操作mysql_mysql

import  pymysql

#创建连接通道
conn=pymysql.connect(host='',user='root',port=3306,password='thinker',db='testdb')
#生成操作mysql的游标
cursor=conn.cursor()

#获取数据
effect_row=cursor.execute('select * from teacher') #并返回影响的函数

print(cursor.fetchone())#打印一条数据

cursor.scroll(1,mode='relative') #修改游标的位置,relative相对位置移动
cursor.scroll(0,mode="absolute") #修改游标位置,absolute绝对位置移动,0代表回到最开始

print(cursor.fetchmany(2)) #打印指定的条数数据
print( cursor.fetchall())#打印所有剩余数据


#插入一行数据
cursor.execute("insert into teacher (teacher_name) values ('chenxiaozan')")
#插入多行数据
cursor.executemany("insert into teacher (teacher_name) values (%s)",['lucas','eric','loss']) #注意这里与python的字符串拼接的%s有些区别,这里是 ,数据

conn.commit() #确认提交
cursor.close() #关闭游标
conn.close() #关闭连接
new_id=cursor.lastrowid #当插入数据时,插入数据那行最新的自增id
print(new_id)

posted on 2020-04-12 00:37  陈小赞  阅读(171)  评论(0编辑  收藏  举报

导航