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