Practical application of database stored procedure

Source: Internet
Author: User

In a Django project, use the stored procedure steps:

① New file: xxx.sh

② write the stored procedure in the xxx.sh file (bold is emphasized):

delimiter// 
drop procedure if exists upload_combo_complete;
CREATE PROCEDURE upload_combo_complete (uid Int,combo VARCHAR (100))

BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE Tmpcount INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER for SQLEXCEPTION SET t_error=1;
START TRANSACTION;
Select Count into Tmpcount from Combocomplete where code = Combo;
IF (Tmpcount > 0) Then
IF uid =-1 Then
Update Combocomplete Set count = tmpcount+1 where code = combo;
ELSE
INSERT INTO Combouidcomplete (uid,code,time) VALUES (Uid,combo,unix_timestamp (now () ));
Update Combocomplete Set count = tmpcount+1 where code = combo;
END IF;
ELSE
IF uid =-1 Then
INSERT INTO Combocomplete (Code,count) values (combo,1);
ELSE
INSERT INTO Combouidcomplete (uid,code,time) VALUES (Uid,combo,unix_timestamp (now () ));
INSERT INTO Combocomplete (Code,count) values (combo,1);
END IF;
END IF;

IF t_error = 1 Then
ROLLBACK;
ELSE
COMMIT;
END IF;
END//

delimiter;

③ Call the stored procedure in the method:
   def    Upload_ Complete_status  (uid,app,sportplandb,combolist):  
for item in Combolist:
sql= "Call upload_combo_complete (%d, '%s ')"% ( Uid,item)
try:
mysql.cal Lproc (sql=sql,db=sportplandb)
return 0
except exception,e:
Logutils.info (' info ', ' M:upload_compl Ete_status,exception is%s '% (e)) return-20003


Supplement: The Mysql.callproc definition in the method is as follows:
@classmethod
Def callproc (CLS, SQL, Params=none, DB=GENERALCONF.SDB):
Try
cursor = Connections[db].cursor ()
Cursor.execute (SQL, params)
Finally
Cursor.close ()

Practical application of database stored procedure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.