SQL 语法错误 C#

发布于 2024-12-27 21:43:33 字数 1235 浏览 1 评论 0原文

我对 C# 相当陌生,我正在尝试制作一个教师管理程序。

这是我用来执行查询的函数。

string commentString = "sC" + (y + 1) + "Y" + (i + 1) + "";
executeQuery("UPDATE student SET " + 
  commentString + " = '" + s.getStudentCourses(i,y,s)+
  "' WHERE sNumber = '" + s.getStudNumber(s) + "'");

我的查询字符串:

query   "UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sNumber = 68721919" string

我得到的异常:

[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 ''sComments1-1' = 'wa5235' WHERE sNumber = 68721919' at line 1"}    MySql.Data.MySqlClient.MySqlException

这是 SQL 数据结构:

CREATE TABLE `NewTable` (
`sNumber`  int(9) NOT NULL ,
`sFirstName`  varchar(32) NOT NULL ,
`sLastName`  varchar(32) NOT NULL ,
`sDOB`  varchar(9) NOT NULL ,
`sGrade`  int(1) NOT NULL ,
`sEmail`  varchar(32) NULL ,
`sParentName`  varchar(32) NOT NULL ,
`sParentPhone`  varchar(11) NOT NULL ,
`sHomeAddress`  varchar(32) NOT NULL ,
`sComments1-1`  varchar(255) NOT NULL ,

使用 MySQL 5.5

我不知道为什么,但这给了我 sql 错误。请帮助我,我的作业两天后到期,我真的需要完成它。

I am fairly new to C#, and i am trying to make a teacher management program.

This is the function that i am using to execute the query.

string commentString = "sC" + (y + 1) + "Y" + (i + 1) + "";
executeQuery("UPDATE student SET " + 
  commentString + " = '" + s.getStudentCourses(i,y,s)+
  "' WHERE sNumber = '" + s.getStudNumber(s) + "'");

My Query String:

query   "UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sNumber = 68721919" string

The exception i get:

[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 ''sComments1-1' = 'wa5235' WHERE sNumber = 68721919' at line 1"}    MySql.Data.MySqlClient.MySqlException

Here is the SQL data structure:

CREATE TABLE `NewTable` (
`sNumber`  int(9) NOT NULL ,
`sFirstName`  varchar(32) NOT NULL ,
`sLastName`  varchar(32) NOT NULL ,
`sDOB`  varchar(9) NOT NULL ,
`sGrade`  int(1) NOT NULL ,
`sEmail`  varchar(32) NULL ,
`sParentName`  varchar(32) NOT NULL ,
`sParentPhone`  varchar(11) NOT NULL ,
`sHomeAddress`  varchar(32) NOT NULL ,
`sComments1-1`  varchar(255) NOT NULL ,

Using MySQL 5.5

I do not know why, but this is giving me sql errors. Please help me, my assignment is due in 2 days and i really need this finished.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぺ禁宫浮华殁 2025-01-03 21:43:33

此处询问并回答您的问题的重复项(列名称中的减号):

MySQL INSERT - 字段名称需要反引号/重音分隔吗?

当在查询中使用带有减号的列名时,您需要在单引号上使用“反引号”。像这样:

UPDATE student SET `sComments1-1` = 'wa5235' WHERE sNumber = 68721919

A duplicate of your problem (minus signs in column names) is asked and answered here:

MySQL INSERT - Do field names require backtick/accent delimination?

You need to use 'back-ticks' instead on single quotes when using the column name with a minus sign in your query. Like this:

UPDATE student SET `sComments1-1` = 'wa5235' WHERE sNumber = 68721919
苄①跕圉湢 2025-01-03 21:43:33

似乎没有任何方法可以从 "sC" + (y + 1) + "Y" + (i + 1) 获取 sComments1-1 ) + "" 所以我不确定为什么你认为你的查询字符串的形式正确。

在任何情况下,您的列名称都需要用反引号而不是单引号引起来:

UPDATE student SET `sComments1-1` = ...

假设 sComments1-1 是拼写错误或早期版本,并且实际上应该是多个之一 表单 sCaYb 的列,其中 ab 是不同的整数,代码将如下所示:

// Get column name of form sC*Y* where *'s are distinct integers.

string commentColumn = "sC" + (y + 1) + "Y" + (i + 1);

// Execute query with correct quote types.

executeQuery("UPDATE student SET `" + commentColumn + "` = '" +
    s.getStudentCourses(i,y,s) +
    "' WHERE sNumber = " + s.getStudNumber(s));

我添加了反引号围绕列名称和sNumber 值中删除它们(因为它是整数而不是字符列)。

There doesn't appear to be any way to get sComments1-1 from "sC" + (y + 1) + "Y" + (i + 1) + "" so I'm not sure why you think your query string is of the correct form.

In any case, your column names need to be surrounded by backticks rather than single quotes:

UPDATE student SET `sComments1-1` = ...

Assuming that sComments1-1 is a typo or earlier version, and should actually be one of multiple columns of the form sCaYb, where a and b are distinct integers, the code would look something like this:

// Get column name of form sC*Y* where *'s are distinct integers.

string commentColumn = "sC" + (y + 1) + "Y" + (i + 1);

// Execute query with correct quote types.

executeQuery("UPDATE student SET `" + commentColumn + "` = '" +
    s.getStudentCourses(i,y,s) +
    "' WHERE sNumber = " + s.getStudNumber(s));

I've added the backticks around the column name and removed them from the sNumber value (since it's an integer rather than a character column).

浪漫人生路 2025-01-03 21:43:33

您是否尝试过删除字段名称周围的单引号?不确定这是否有效,但这是我会尝试的一件事。

如果这没有帮助,请告诉我,我将删除此答案

Have you tried removing the single quotes around your field names? unsure this will work but it's one thing i'd try.

if this doesn't help, let me know and i'll remove this answer

把梦留给海 2025-01-03 21:43:33
"UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sN....

列名“sComments1-1”不应作为带引号的字符串,只需编写不带引号的列名称即可。

"UPDATE student SET 'sComments1-1' = 'wa5235' WHERE sN....

The Columnname 'sComments1-1' shouldnt be as a quoted string, just write the column name without the Quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文