我正在尝试将数据插入到名为 game 的表中,但出现以下错误: 您的 SQL 语法有错误,请查看与您的 MySQL 服务器对应的手册
这是 C# 代码:
int i = 0;
MySqlCommand cmd = connection.CreateCommand();
string command = string.Format("insert into game (HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR,HS,AS,HST,AST,HF,AF,HC,AC,HY,AY,HR,AR,DIV) VALUES(@HomeTeam,@AwayTeam,@FTHG,@FTAG,@FTR,@HTHG,@HTAG,@HTR,@HS,@AS,@HST,@AST,@HF,@AF,@HC,@AC,@HY,@AY,@HR,@AR,@DIV)");
cmd.CommandText = command;
cmd.Parameters.AddWithValue("@HomeTeam", _games[i].HomeTeam.ToString());
cmd.Parameters.AddWithValue("@AwayTeam", _games[i].AwayTeam.ToString());
cmd.Parameters.AddWithValue("@FTHG", Convert.ToInt32(_games[i].FTHG));
cmd.Parameters.AddWithValue("@FTAG", Convert.ToInt32(_games[i].FTAG)); ;
cmd.Parameters.AddWithValue("@FTR", _games[i].FTR.ToString());
cmd.Parameters.AddWithValue("@HTHG", Convert.ToInt32(_games[i].HTHG));
cmd.Parameters.AddWithValue("@HTAG", Convert.ToInt32(_games[i].HTAG));
cmd.Parameters.AddWithValue("@HTR", _games[i].HTR.ToString());
cmd.Parameters.AddWithValue("@HS", Convert.ToInt32(_games[i].HS));
cmd.Parameters.AddWithValue("@AS", Convert.ToInt32(_games[i].AS));
cmd.Parameters.AddWithValue("@HST", Convert.ToInt32(_games[i].HST));
cmd.Parameters.AddWithValue("@AST", Convert.ToInt32(_games[i].AST));
cmd.Parameters.AddWithValue("@HF", Convert.ToInt32(_games[i].HF));
cmd.Parameters.AddWithValue("@AF", Convert.ToInt32(_games[i].AF));
cmd.Parameters.AddWithValue("@HC", Convert.ToInt32(_games[i].HC));
cmd.Parameters.AddWithValue("@AC", Convert.ToInt32(_games[i].AC));
cmd.Parameters.AddWithValue("@HY", Convert.ToInt32(_games[i].HY));
cmd.Parameters.AddWithValue("@AY", Convert.ToInt32(_games[i].AY));
cmd.Parameters.AddWithValue("@HR", Convert.ToInt32(_games[i].HR));
cmd.Parameters.AddWithValue("@AR", Convert.ToInt32(_games[i].AR));
cmd.Parameters.AddWithValue("@DIV", _games[i].DIV.ToString());
cmd.ExecuteNonQuery();
HomeTeam、AwayTeam、DIV、FTR、HTR 都是 varchars,所有第一个都是 int。 在列表中,所有字段都是字符串。
有人看到这里有什么错误吗?
最佳答案
您在列名称处有“AS”和“DIV”关键字。 使用 `
转义它们试试这个:
insert into game (HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR,HS,`AS`,HST,AST,HF,AF,HC,AC,HY,AY,HR,AR,`DIV`) VALUES(@HomeTeam,@AwayTeam,@FTHG,@FTAG,@FTR,@HTHG,@HTAG,@HTR,@HS,@AS,@HST,@AST,@HF,@AF,@HC,@AC,@HY,@AY,@HR,@AR,@DIV);
关于c# - 将数据插入名为 game 的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792214/