import pymysql #數據庫查詢 class QuerySqlData(object): def __init__(self): self.conn = pymysql.connect( host='ip', port=3306, user='#', passwd='#', db='#', charset='utf8') # 建立鏈接 def find_table(self,sql,bid): cursor = self.conn.cursor() # 建立鏈接 try: cursor.execute(sql, bid) # 執行SQL results = cursor.fetchall(); # 返回查詢數據 self.conn.commit() # 提交,否則沒法保存新建或者修改的數據 except: print("數據鏈接錯誤-find_table") finally: cursor.close() # 關閉遊標 self.conn.close() # 關閉鏈接 return resultsif __name__ == '__main__': querySqlData = QuerySqlData() value = {"userid":"aa"} a = querySqlData.find_table('SELECT * from t_user u where u.userid=%(userid)s',value) print(a)