C# - INSERT进入DB的MySQL语法错误

时间:2013-09-13 20:36:09

标签: c# mysql sql

我在程序中遇到MySqlException错误。这是一个简单的测试程序,我试图将带有时间戳的日志错误插入到我的数据库中的表中。

// ------------- PROGRAM ----------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;  


string Log = "Sample Log Error";
string key = "11111";

string date;
string time;
int unixT;
TimeSpan unix_time;

// geting current date in yyyy-mm-dd format     
date = DateTime.UtcNow.ToString("yyyy-MM-dd"); 

//getting current time in hh:mm:ss format
time = string.Format("{0:HH:mm:ss tt}", DateTime.Now); 

// Getting Unix Time
unix_time = (System.DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); 

//converting Unix time to seconds
unixT = Convert.ToInt32(unix_time.TotalSeconds); 

MySqlConnection myConn = new MySqlConnection("Server=172.178.2.143;" +            "Port=3306;" + "Database=DB;" + "Uid=user;" + "Pwd=pass;");                

 try
 {
   myConn.Open();
 }
 catch (MySqlException e)
 {
   Console.WriteLine(e);
 }                   

 MySqlCommand comm1 = new MySqlCommand("INSERT INTO Logger(date_stamp,         time_stamp, log, unix_time, key) VALUES(@date_stamp, @time_stamp, @log, @unix_time, @key)", myConnection);

 //inserting each element from GPS split message into its respective table    and column in the database                
comm1.Parameters.AddWithValue("date_stamp", date);              
comm1.Parameters.AddWithValue("time_stamp", time);                
comm1.Parameters.AddWithValue("log", Log);                
comm1.Parameters.AddWithValue("unix_time", unixT);
comm1.Parameters.AddWithValue("key", key);

try
{
  comm1.ExecuteNonQuery(); //executing the INSERT Query Above
}

catch (MySqlException e)               
{                    
  Console.WriteLine(e);                
}

myConnection.Close();

// ------------ END PROGRAM ------------------

我收到以下错误:

  

“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'key'附近使用正确的语法)VALUES('2013-09-13','16:21:15 PM ','采样记录错误',1379103676,'11111''在第1行“

2 个答案:

答案 0 :(得分:1)

单词KEY是MySql的reserved keyword

如果您想在代码中使用该名称,则需要在其周围添加反引号

 MySqlCommand comm1 = new MySqlCommand("INSERT INTO Logger(date_stamp," + 
                          "time_stamp, log, unix_time, `key`) " + 
                          "VALUES(@date_stamp, @time_stamp, @log, @unix_time, @key)",
                           myConnection);

如果在您的应用程序的开发阶段还为时已晚,我建议您更改该名称,因为每次触摸此字段时都会遇到同样的问题。

我还希望建议您的代码周围using statement,只需一次尝试抓住

try
{
    using(MySqlConnection myConn = new MySqlConnection(.....))
    using(MySqlCommand comm1 = new MySqlCommand(......))
    {
         myConn.Open();
         comm1.Parameters.AddWithValue("date_stamp", date);              
         .....
         comm1.ExecuteNonQuery(); 
    }
}
catch (MySqlException e)               
{                    
    Console.WriteLine(e);                
}

通过这种方式,您可以正确处理边缘情况(无法打开连接,无法执行命令),并且当代码流出结束括号时,您的连接将被正确关闭和处理。

答案 1 :(得分:0)

key是一个保留字,用反引号包装,以便MySQL知道你在谈论列名。