import pymysql from pymysql.cursors import DictCursor def con(host='127.0.0.1',user='root',password='',port=3306,db='login',charset='utf8'): # # 創建連接 conne=pymysql.connect(host=host,user=user,password=password,port=port,db=db,charset=charset) # 創建游標 cursor=conne.cursor(cursor=DictCursor) #<class 'tuple'> return conne,cursor def sql(sql): c,corsor=con() # 執行sql corsor.execute(query=sql) if sql[:6]=='select' : result = corsor.fetchall() if result: # <class 'list'> # print(result) corsor.close() c.close() return result else: return 0 # 增刪改需要提交 else: c.commit() corsor.close() c.close() return 1 if __name__ == '__main__': a= f'UPDATE stu SET PASSWORD=\'qq\' WHERE sname=\'qq\'' c=sql(a)