2017-04-09 9 views
0

Ich schreibe ein kleines Browserspiel mit einer InnoDB Datenbank. Um das Zeug zu automatisieren, das passiert, wenn Sie ein neues Haus bauen, schrieb ich das folgende Verfahren. Das Problem dabei ist, dass jede deklarierte Variable außer "cycle_offset" nach dem ersten SELECT noch NULL ist, so dass die drei folgenden Selects nicht ausgeführt werden können, ohne selbst NULL zu liefern.einige Variablen bleiben null - gespeicherte Prozedur MySQL

DELIMITER // 
DROP PROCEDURE IF EXISTS create_building // 
CREATE PROCEDURE create_building(IN userID INT, IN buildingID INT) 
BEGIN 
--declare variables 
DECLARE cycle_offset INT; 
DECLARE res_1 INT; 
DECLARE am_1 INT; 
DECLARE res_2 INT; 
DECLARE am_2 INT; 
DECLARE res_3 INT; 
DECLARE am_3 INT; 
DECLARE u_am_1 INT; 
DECLARE u_am_2 INT; 
DECLARE u_am_3 INT; 

--fill the values that belong to the certain type of building 
SELECT cycles * 10, 
     res_1, 
     am_1, 
     res_2, 
     am_2, 
     res_3, 
     am_3 
INTO cycle_offset, 
     res_1, 
     am_1, 
     res_2, 
     am_2, 
     res_3, 
     am_3 
FROM build_cost 
WHERE building = buildingID; 

--select resources from user-fortune that were given in the build-cost 
SELECT amount INTO u_am_1 FROM fortune WHERE resource = res_1; 
SELECT amount INTO u_am_2 FROM fortune WHERE resource = res_2; 
SELECT amount INTO u_am_3 FROM fortune WHERE resource = res_3; 
--for each resource: if the resource is not null and the user has more resources left than needed 
--remove the "price" from the user's fortune 
IF 
    ((res_1 IS NOT NULL) AND 
    (u_am_1 >= am_1)) 
THEN 
    UPDATE fortune 
    SET amount = (u_am_1 - am_1) 
    WHERE resource = res_1 AND 
      user = userID; 
END IF; 

IF 
    ((res_1 IS NOT NULL) AND 
    (u_am_2 >= am_2)) 
THEN 
    UPDATE fortune 
    SET amount = (u_am_2 - am_2) 
    WHERE resource = res_2 AND 
      user = userID; 
END IF; 

IF 
    ((res_1 IS NOT NULL) AND 
    (u_am_3 >= am_3)) 
THEN 
    UPDATE fortune 
    SET amount = (u_am_3 - am_3) 
    WHERE resource = res_3 AND 
      user = userID; 
END IF; 

--add the building into the users town 

INSERT INTO town (user, building, cycle_start, level, in_construction) 
VALUES (userID, 
     buildingID, 
     SEC_TO_TIME(FLOOR((TIME_TO_SEC(CURRENT_TIME)+5)/10) * 10 + cycle_offset), 
     1, 
     1); 

--debugging purposes: 
INSERT INTO debug (a,b,c,d,e,f,g,h,i,j,k,l) VALUES (userID, buildingID, cycle_offset, res_1, am_1, res_2, am_2, res_3, am_3, u_am_1, u_am_2, u_am_3); 
END // 

Die Debug-Tabelle zeigt mir immer, dass Benutzer-ID, buildingID und Takt_Offset nicht null ist, ist der Rest. Man könnte denken, dass die „build_cost“ -Tabelle leer ist, aber allein gibt mir dieses Ergebnis die Abfrage ausgeführt wird (wie erwartet):

SELECT cycles * 10, res_1, am_1, res_2, am_2, res_3, am_3 
FROM build_cost 
WHERE building = 1; 

+-------------+-------+------+-------+------+-------+------+ 
| cycles * 10 | res_1 | am_1 | res_2 | am_2 | res_3 | am_3 | 
+-------------+-------+------+-------+------+-------+------+ 
|   0 |  4 | 50 |  5 | 50 | NULL | NULL | 
+-------------+-------+------+-------+------+-------+------+ 

Warum sind die Werte für die Zyklen, RES_1, AM_1, res_2 und am_2 nicht gespeichert in meinen deklarierten Variablen, wenn ich die Prozedur ausführen, die diese Abfrage enthält?

+0

ich, wo Sie res_3 oder am_3 auf einen Wert setzen nicht überall in Ihrem Code. –

+0

SELECT Zyklen * 10, RES_1, AM_1, res_2, am_2, res_3, am_3 INTO Takt_Offset, RES_1, AM_1, res_2, am_2, res_3, am_3 FROM build_cost WHERE building = buildingID; ' Es sollte hier passieren, oder ist das falsch? – cscholz

+0

Nur wenn in diesen Spalten Werte vorhanden sind. –

Antwort

0

sah ich in einem Debugger, der MySQL

DECLARE cycle_offset INT; 
DECLARE res_1 INT; 
DECLARE am_1 INT; 
DECLARE res_2 INT; 
DECLARE am_2 INT; 
DECLARE res_3 INT; 
DECLARE am_3 INT; 

SELECT cycles * 10, 
     res_1, 
     am_1, 
     res_2, 
     am_2, 
     res_3, 
     am_3 
INTO cycle_offset, 
     res_1, 
     am_1, 
     res_2, 
     am_2, 
     res_3, 
     am_3 
FROM build_cost 
WHERE building = buildingID; 

als

SELECT cycles * 10, 
     NULL, 
     NULL, 
     NULL, 
     NULL, 
     NULL, 
     NULL 
INTO cycle_offset, 
     res_1, 
     am_1, 
     res_2, 
     am_2, 
     res_3, 
     am_3 
FROM build_cost 
WHERE building = buildingID; 

interpretes, weil sie, zumindest sieht es so aus, kann zwischen den Variablen und den Tabellenspalten nicht unterscheiden, ob sie sind gleich benennen.

Danke für die Mühe, obwohl

Verwandte Themen