我已经阅读了很多线程,但是它们都没有真正起作用,这就是为什么我要问一个新问题。
好吧,所以我试图在我的MySQL数据库中插入值,但出现错误。
MySql.Data.MySqlClient.MySqlException:'您的SQL错误 句法;检查与您的MySQL服务器版本相对应的手册 在'Desc,Detectors,DetectorNos,附近使用正确的语法, 第1行中的第1个问题,第2个问题,SpecialPrec,OfficialSign,
我找不到问题所在。我已经尝试了好几个小时没有任何结果,甚至读了一百遍,但还是没有运气。所以我需要别人看看。
public string detector = "";
public string questions = "";
public string question2 = "";
public string capOrCheif = "";
private void btn_send_Click(object sender, EventArgs e)
{
if(cbox_detectors_yes.Checked == true)
{
detector = "Yes";
}
if(cbox_yes1.Checked == true && cbox_yes3.Checked == true && cbox_yes4.Checked == true && cbox_yes5.Checked == true && cbox_yes6.Checked == true && cbox_yes7.Checked == true)
{
questions = "Yes";
}
if(cbox_yes2.Checked == true)
{
question2 = "Yes";
}
if(cbox_cheif.Checked == true)
{
capOrCheif = "Cheif Engineer";
}
else if(cbox_captain.Checked == true)
{
capOrCheif = "Captain";
}
else if(cbox_na2.Checked == true)
{
question2 = "N/A";
}
else if(cbox_detectors_na.Checked == true)
{
detector = "N/A";
}
string constring = "Server = **; Database = **; User Id = **; Password = ***; Sslmode = none;";
string Query = "INSERT INTO tbl_permit (Username, Ship, Date, TimeFrom, TimeTo, Location, Desc, Detectors, DetectorNos, Question1, Question2, SpecialPrec, OfficerSign, OfficerName, OfficerPos, CheifSign, CheifName, CaptainSign, CaptainName, PrecAddedBy, PrecBox) values(@Username, @Ship, @Date, @TimeFrom, @TimeTo, @Location, @Desc, @Detectors, @DetectorNos, @Question1, @Question2, @SpecialPrec, @OfficerSign, @OfficerName, @OfficerPos, @CheifSign, @CheifName, @CaptainSign, @CaptainName, @PrecAddedBy, @PrecBox);";
MySqlConnection con = new MySqlConnection(constring);
MySqlCommand cmdDatabase = new MySqlCommand(Query, con);
cmdDatabase.Parameters.Add("@Username", MySqlDbType.VarChar, 50).Value = login.username;
cmdDatabase.Parameters.Add("@Ship", MySqlDbType.VarChar, 50).Value = txtbox_ship.Text;
cmdDatabase.Parameters.Add("@Date", MySqlDbType.VarChar, 50).Value = txtbox_date.Text;
cmdDatabase.Parameters.Add("@TimeFrom", MySqlDbType.VarChar, 50).Value = txtbox_timeFrom.Text;
cmdDatabase.Parameters.Add("@TimeTo", MySqlDbType.VarChar, 50).Value = txtbox_timeTo.Text;
cmdDatabase.Parameters.Add("@Location", MySqlDbType.VarChar, 50).Value = txtbox_location;
cmdDatabase.Parameters.Add("@Desc", MySqlDbType.VarChar, 50).Value = txtbox_work_desc.Text;
cmdDatabase.Parameters.Add("@Detectors", MySqlDbType.VarChar, 50).Value = detector;
cmdDatabase.Parameters.Add("@DetectorNos", MySqlDbType.VarChar, 50).Value = txtbox_detector_desc.Text;
cmdDatabase.Parameters.Add("@Question1", MySqlDbType.VarChar, 50).Value = questions;
cmdDatabase.Parameters.Add("@Question2", MySqlDbType.VarChar, 50).Value = question2;
cmdDatabase.Parameters.Add("@SpecialPrec", MySqlDbType.VarChar, 50).Value = txtbox_precautions.Text;
cmdDatabase.Parameters.Add("@OfficerSign", MySqlDbType.VarChar, 50).Value = txtbox_officer_sign.Text;
cmdDatabase.Parameters.Add("@OfficerName", MySqlDbType.VarChar, 50).Value = txtbox_officer_name.Text;
cmdDatabase.Parameters.Add("@OfficerPos", MySqlDbType.VarChar, 50).Value = txtbox_officer_pos.Text;
cmdDatabase.Parameters.Add("@CheifSign", MySqlDbType.VarChar, 50).Value = txtbox_cheif_sign.Text;
cmdDatabase.Parameters.Add("@CheifName", MySqlDbType.VarChar, 50).Value = txtbox_cheif_name.Text;
cmdDatabase.Parameters.Add("@CaptainSign", MySqlDbType.VarChar, 50).Value = txtbox_captain_sign.Text;
cmdDatabase.Parameters.Add("@CaptainName", MySqlDbType.VarChar, 50).Value = txtbox_captain_name.Text;
cmdDatabase.Parameters.Add("@PrecAddedBy", MySqlDbType.VarChar, 50).Value = capOrCheif;
cmdDatabase.Parameters.Add("@PrecBox", MySqlDbType.VarChar, 50).Value = txtbox_restrictions.Text;
MySqlDataReader myReader;
if (cbox_read.Checked == true)
{
con.Open();
myReader = cmdDatabase.ExecuteReader();
while (myReader.Read())
{
}
MessageBox.Show("Hot Work Permit Form has been sent to the Cheif Engineer");
}
else
{
MessageBox.Show("You have to read it through and accept it!");
}
}
答案 0 :(得分:3)
DATE
不是 保留字。您可以在此处获取保留字的列表:https://dev.mysql.com/doc/refman/8.0/en/keywords.html
该页面列出了关键字,但仅保留了其中一部分,由列表中的(R)
注释指示。
错误消息告诉您哪个词使解析器感到困惑:
...检查与您的MySQL服务器版本相对应的手册,以获取在'Desc,Detectors,...附近使用的正确语法。
它对DESC
一词感到困惑。 MySQL中的语法错误总是向您显示查询开始混淆的部分。
DESC
是在这种情况下引起问题的保留字。在我链接到的关键字列表中,DESC
带有(R)
注释。
您应界定与保留字冲突的标识符:
string Query = "INSERT INTO tbl_permit (... Location, `Desc`, Detectors, ...