当前位置: 移动技术网 > IT编程>数据库>Mysql > pymysql模块

pymysql模块

2019年07月22日  | 移动技术网IT编程  | 我要评论

07.21自我总结

pymysql模块

一.创建连接库

conn = pymysql.connect(host="127.0.0.1",#默认是本机
                       port=3306, #默认3306
                       user="root",#必填
                       password='密码',#必填
                       db="库名")#必填
#如果没有库会报pymysql.err.internalerror: (1049, "unknown database '库名'")
所有我们编辑可以这样
try:
    conn = pymysql.connect(host="127.0.0.1",#默认是本机
                           port=3306, #默认3306
                           user="root",#必填
                           password='16745',#必填
                           db="asds",)#必填
except pymysql.err.internalerror:
    print('没有库')

二.建立游标

cursor = conn.cursor(pymysql.cursors.dictcursor) #自定义游标类型为字典
cursor = conn.cursor()#默认是元祖

三.提交sql语句

普通提交

count = cursor.execute('show tables') #返回值为受到影响的数据条数

防注入提交

table_name = input('table name :')
count = cursor.execute('select table %s',(name,))

四.查看内容

默认显示之前那一次显示的内容,只显示查看相关语法的内容,为返回值 
cursor.fetchall() #查看全部
cursor.fetchone() #查看当前游标位置的一个值
cursor.fetchmay(n) #查看当前游标位置的n值

五.移动游标

  • 相对位置

    cursor.scroll(1, "relative")

    cursor.scroll() #默认是相对位置

  • 绝对位置

    cursor.scroll(0, "absolute")

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网