码迷,mamicode.com
首页 > 数据库 > 详细

pymysql模块

时间:2019-07-21 16:46:01      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:error:   ctc   fetch   chm   位置   class   ict   show   code   

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")

pymysql模块

标签:error:   ctc   fetch   chm   位置   class   ict   show   code   

原文地址:https://www.cnblogs.com/pythonywy/p/11221429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!