Search code examples
mysqlstored-proceduressql-updatesubstringtic-tac-toe

Tic-Tac-Toe Game Unknown column 'i' in field list MySQL


There is a part of my Tic-tac-toe game that gathers the beginning user's move and then updates the board. The move variable starts as a string so I use substring to get the column and row out of the string and put them into two variables i and j. Then I use an update statement to change the board. When I do so, however, it comes back with the error saying "Unknown column 'i' in field list". I don't understand why as I have declared the two variables at the top of the procedure.

EDIT: Here is the code for the table I am using:

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);

Here's the code:

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;

Any help given will be appreciated!


Solution

  • If I had this schema, hypothetically. Well now I do.

    create table grid
    (
        ttt int not null
    );
    

    And this is my stored proc

    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 ;
    

    And I call this

    call asdf('ignore');
    

    I get that exact error message

    Error Code: 1054. Unknown column 'i' in 'field list'

    So i is not a column in table grid