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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-10-03
猜你喜欢
  • 2022-12-23
  • 2021-08-03
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
相关资源
相似解决方案