Python通過呼叫mysql儲存過程實現更新資料功能示例
阿新 • • 發佈:2020-01-09
本文例項講述了Python通過呼叫mysql儲存過程實現更新資料功能。分享給大家供大家參考,具體如下:
一、需求分析
由於管理費率配置錯誤,生成訂單的還本付息表和訂單表的各種金額,管理費之間的計算都有錯誤,需要進行資料訂正。為此,為了造個輪子,以後省很多功夫,全部用程式去修正,不接入人工。
二、帶引數mysql 儲存過程建立
1、更新訂單付息表(t_order_rapay)
drop procedure if exists update_t_order_rapay; delimiter $$ create procedure update_t_order_rapay(IN orderNo varchar(64)) begin declare t_order_no varchar(64); set t_order_no=orderNo; UPDATE t_order_repay SET total_amount=principal+interest+round(manage_amount*0.0808/0.052,3)+breach_amount,left_amount=principal+interest+round(manage_amount*0.0808/0.052,left_repay_manager=round(manage_amount*0.0808/0.052,3),manage_amount=round(manage_amount*0.0808/0.052,3) WHERE order_no=t_order_no; end $$ delimiter;
2、更新訂單表(t_order_info)
drop procedure if exists update_t_order_info; delimiter $$ create procedure update_t_order_info(IN orderNo varchar(64)) begin declare t_order_no varchar(64); set t_order_no=orderNo; SELECT left_amount into @m1 from t_order_repay WHERE order_no=t_order_no ORDER BY plan_time LIMIT 1; UPDATE t_order_info set manage_cost_rate=0.0808,manage_cost=round(manage_cost*0.0808/0.052,left_amount=borrow_amount+interest_amount+manage_cost,next_amount_need=@m1 WHERE order_no=t_order_no; end $$ delimiter;
3、python 呼叫mysql 中的儲存過程
# encoding: utf-8 import time import sys reload(sys) sys.setdefaultencoding('utf-8') time1 = time.time() import pandas as pd import pymysql ############################################從資料庫讀資料########################################### ###########線上###################### # 加上字符集引數,防止中文亂碼 # conn=pymysql.connect( # host="##########",# database="######",# user="####",# password="##########",# port=#######,# charset='utf8' # ) # #############測試庫###################### # ## 加上字符集引數,防止中文亂碼 # conn=pymysql.connect( # host="172.16.34.32",# database="#########",# user="admin",# password="##############",# port=#########,# charset='utf8' # ) #sql語句(安徽) # sqlcmd=""" # SELECT order_no from t_order_info WHERE offline_org_no in( # 0032,0035,0036 # # # ) and substr(create_time,1,10)>="2017-10-31" # # and `status` in(105,106,107,108) # # and manage_cost_rate=0.052 # # # """ #################sql語句(江蘇) # sqlcmd2=""" # SELECT order_no from t_order_info WHERE offline_org_no in( # 0002,0005,0006,0007,0008,0009,0010,0011,0012,0013,0014,0017,0018,0019,0025,0026,0027,0028,0030,0031,0033,0034 # ) and substr(create_time,10)>="2017-10-31" # and `status` in(105,108) # and manage_cost_rate=0.052 # # """ #利用pandas 模組匯入mysql資料 # data=pd.read_sql(sqlcmd2,conn) # print data # # # ###################更新order_rapay表 # for each in data["order_no"]: # print each # # 建立遊標 # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # #有引數儲存過程 # cursor.execute('call update_t_order_rapay(%s)',(each)) # conn.commit() # # print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" # #####################更新order_info表################### # for each in data["order_no"]: # print each # # # 建立遊標 # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # #有引數儲存過程 # cursor.execute('call update_t_order_info(%s)',(each)) # conn.commit() # # # cursor.close() # print '呼叫儲存過程完畢................' # conn.close() # time2=time.time() # print u'總共耗時:' + str(time2 - time1) + 's'
更多關於Python相關內容感興趣的讀者可檢視本站專題:《Python+MySQL資料庫程式設計入門教程》、《Python常見資料庫操作技巧彙總》、《Python數學運算技巧總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》及《Python檔案與目錄操作技巧彙總》
希望本文所述對大家Python程式設計有所幫助。