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