Jordan Fleetwood
Jordan Fleetwood

Reputation: 31

MySQL server error - You have an error in your SQL syntax

I'm trying to update a Database table and getting the error

"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;
    }

}

Upvotes: 0

Views: 435

Answers (2)

J.H
J.H

Reputation: 116

I tried your query on https://sqltest.net/ and noticed it highlighted "group" when I tried to create the table. I'm wondering if the problem might be the usage of "group" as a column name since it's a reserved word.

Is it possible to try renaming the column to group_level or adding back ticks around 'group' or "group" and seeing if that works?

So for example

'group'=@grouplevel

I found this thread and this thread on renaming the column where they had issues with "group" as a column name. Adding backticks seemed to solve both problems.

EDIT: As per OP, double quotes (") solved the issue instead of single. Edited answer to include.

Upvotes: 3

Arphile
Arphile

Reputation: 861

Try change query like this

String query = "UPDATE " + table + " SET job='@jobTitle', job_grade=@jobGrade, group='@adminLevel' WHERE identifier='@identifier'";

if you input String value with query, you need to use 'this' for work

I hope this will work for you.

if not, you can use String.Format for that like this.

String Query = String.Format("Update `{0}` Set job='{1}', job_grade={2}, group='{3}' Where identifier='{4}'", table, jobTitle, jobGrade, adminLevel, identifier);

Upvotes: -1

Related Questions