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()
mysql查詢慢用遊式遊標
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.