首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >mysql查询慢用游式游标

mysql查询慢用游式游标

作者头像
小小咸鱼YwY
发布2021-06-10 15:58:51
发布2021-06-10 15:58:51
2.8K0
举报
文章被收录于专栏:python-爬虫python-爬虫
代码语言:javascript
复制
import pymysql

class MySQLSnippet:

    def __init__(self):
        # 游式游标
        self.connect_settings = {
            "host": "127.0.0.1",
            "port": 3306,
            "user": "root",
            "passwd": "root",
            "db": "dbname",
        } # 配置数据库链接参数
        self.connection = pymysql.connect(**self.connect_settings)
        self.cursor = self.connection.cursor(cursor=pymysql.cursors.SSDictCursor)
        # self.cursor = self.connection.cursor(cursor=pymysql.cursors.DictCursor)

    def dosomething(self):
        select_sql = "SELECT NEWS_TITLE, NEWS_URL FROM `vnews_content_wechat` WHERE NEWS_PUBLISH_TIME between '2020-11-02 00:00:00' and '2020-11-09 00:00:00';"

        self.cursor.execute(select_sql)
        # 1. for 循环的方式
        for cur in self.cursor:
            print(cur)
        
        # 2. while 循环的方式
        while True: 
            data = self.cursor.fetchone()
            if data:
                Ellipsis
            else:
                break
                
    def close(self):
        # 关闭游标
        self.cursor.close()
        self.connection.close()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档