我正在尝试更新数据库表并收到错误
"MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group='superadmin' WHERE identifier='steam:steam:1100001098b5888'' at line 1'"
// Creates query to run
public void UpdateInfo(String jobTitle, int jobGrade, String adminLevel, String identifier) {
// Opens the database connection if it's not already open
if (!(databaseConnected)) {
openConnection();
}
// Creates query to run
String query = "UPDATE " + table + " SET job=@jobTitle, job_grade=@jobGrade, group=@adminLevel WHERE identifier=@identifier";
// Makes a new command
MySqlCommand cmd = new MySqlCommand(query, connection);
// Replaces the @ placeholders with actual variables
cmd.Parameters.AddWithValue("@jobTitle", jobTitle);
cmd.Parameters.AddWithValue("@jobGrade", jobGrade);
cmd.Parameters.AddWithValue("@adminLevel", adminLevel);
cmd.Parameters.AddWithValue("@identifier", identifier);
// Executes it and if it's...
if (cmd.ExecuteNonQuery() > 0) {
// Successful
MessageBox.Show("Successfully updated information");
closeConnection();
return;
} else {
// Not successful
MessageBox.Show("Error with updating information!");
// Closes the connection again to prevent leaks
closeConnection();
return;
}
}
最佳答案
我在 https://sqltest.net/ 上尝试了您的查询当我尝试创建表格时,注意到它突出显示了“组”。我想知道问题是否可能是使用“group”作为列名,因为它是保留字。
是否可以尝试将列重命名为 group_level 或在“group”或“group”周围添加反勾号,看看是否有效?
举个例子
'group'=@grouplevel
我找到了this thread和 this thread on renaming the column他们对“group”作为列名有疑问。添加反引号似乎可以解决这两个问题。
编辑:根据OP,双引号(“)解决了问题而不是单引号。编辑后的答案包括在内。
关于c# - MySQL 服务器错误 - 您的 SQL 语法有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283649/