1. 程式人生 > 其它 >【python】pymysql庫的簡單使用

【python】pymysql庫的簡單使用

import pymysql
#連線資料庫
class lgx:
def __init__(self):
self.connect1 = pymysql.connect(host='',
# 地址
user='',
# 使用者名稱
password='',
#密碼
db='',
# 資料庫名
charset='utf8',
#格式處理
cursorclass=pymysql.cursors.DictCursor,
#返回內容字典化,便於檢視瀏覽,不加的話返回元組
autocommit=False
#阻斷自動提交,防止出錯時產生髒資料
)

def lgxdsb(self):
# 做一個游標
cursor1 = self.connect1.cursor()
#寫入想要執行資料庫操作語句
sql="""
select id,site_id,error_code from union_hybrid_event_log_test where id=1;
"""
# 執行這個語句
cursor1.execute(sql)
# 涉及到修改資料庫時要加上·提交commit()
# connect1.commit()
# 輸出查詢後儲存在內部的資料
print(cursor1.fetchall())
# 關閉游標
cursor1.close()
# 關閉資料庫連線
self.connect1.close()
if __name__ == '__main__':
SQL=lgx().lgxdsb()