2015-11-28 35 views
0

我的井字遊戲有一部分收集開始用戶的移動,然後更新板。移動變量以字符串開頭,所以我使用substring將字符串和列從字符串中取出,並將它們放入兩個變量i和j中。然後我使用更新聲明來更換電路板。但是,當我這樣做時,它會返回錯誤,並在字段列表中顯示「未知列」。我不明白爲什麼,因爲我已經在程序的頂部聲明瞭兩個變量。Tic-Tac-Toe遊戲在字段列表中的未知列'i'MySQL

編輯:這是我使用的表的代碼:

CREATE TABLE `tictactoe_4213`.`grid` 
(`TTT` INT NOT NULL, 
`A` VARCHAR(45) NULL, 
`B` VARCHAR(45) NULL, 
`C` VARCHAR(45) NULL, 
    PRIMARY KEY (`TTT`)); 

Insert into grid 
values (1, null, null, null), 
(2, null, null, null), 
(3, null, null, null); 

下面的代碼:

create procedure tictactoe(input varchar(4)) 
begin 

declare i varchar(1); 
declare j varchar(1); 

...

if input like 'bu__' then 

    select substring(input, 3, 1) into i; 
    select substring(input, 4, 1) into j; 

if i not in ('A', 'B', 'C') then 
    select 'Invalid Letter. Please enter a letter between A and C for the column.'; 
end if; 

if j not in (1, 2, 3) then 
    select 'Invalid Number. Please enter a number between 1 and 3 for the row.'; 
end if; 

update grid 
set i = 'U' 
where ttt = j; 

select * from grid; 
end if; 

給予任何幫助將是不勝感激!

+0

你能提供什麼表,你所得到的是錯誤的表定義? 'mysql -u username -p''use database'' describe table' – kshikama

回答

0

如果我有這個模式,假設。我現在好了。

create table grid 
(
    ttt int not null 
); 

這是我的存儲過程

drop procedure if exists asdf; 

delimiter $$ 
create procedure asdf(input varchar(40)) 
begin 

declare i varchar(1); 
declare j varchar(1); 

set i='a'; 

update grid 
set i = 'U' 
where ttt = j; 

if i not in ('A', 'B', 'C') then 
    select 'hello' as aMsg; 
else 
    select '2222' as aMsg; 
end if; 

end 
$$ 
delimiter ; 

而且我把這種

call asdf('ignore'); 

我得到確切的錯誤信息

錯誤代碼:1054未知列'i'in'field list'

所以i是不是在表中的列grid

+0

所以看到你對問題的更新,我是對的。你在表格'grid'中沒有列'i'。您的更新聲明失敗。不要混淆變量名和列名。 – Drew

+0

那麼你建議我做什麼呢?這是我能想到的唯一的事情。 – RyuKaze78

+0

好吧,你甚至不提供你的存儲過程。你想讓我做什麼 – Drew