MySQL在字段列表中插入未知列

时间:2015-06-27 18:36:41

标签: java mysql

我正在

  

MySQLSyntaxErrorException:未知列' UUID'在'字段列表'

使用此代码:

String sql = "INSERT INTO players (player_uuid , name , age , gender , bios) " +
            "VALUES ( `UUID` , " + " `TestName` , 8 , `Female` , `TestBios`)";

我已经创建了这个表。

4 个答案:

答案 0 :(得分:4)

不要以这种方式编写查询,它是不安全的(SQL注入),并且可能比以下方法慢很多:

对包含变量的查询使用预准备语句:

String sql="INSERT INTO players (player_uuid , name , age , gender , bios) "+
" VALUES (?,?,?,?,?)";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,"UUID");
pstmt.setString(2,"TestName");
pstmt.setint(3,8);
pstmt.setString(4,"Female");
pstmt.setString(5,"TestBios");
pstmt.execute();
pstmt.close();

数据库将缓存预准备语句的执行计划,这将在下次使用相同语句时节省时间(几乎不管值如何,但某些数据库将根据值调整计划)。 使用刀片,在大多数情况下节省的成本都很低,选择总体节省可能很大。

答案 1 :(得分:1)

您需要将值设置为引号,以将它们视为字符串。

/// The `Measurement` trait and the `implement_measurement!` macro
/// provides a common way for various measurements to be implemented.
///
/// # Example
/// ```
/// // Importing the `implement_measurement` macro from the external crate is important
/// #[macro_use]
/// extern crate measurements;
/// 
/// use measurements::measurement::*;
/// 
/// struct Cubits {
///     forearms: f64
/// }
/// 
/// impl Measurement for Cubits {
///     fn get_base_units(&self) -> f64 {
///         self.forearms
///     }
///     
///     fn from_base_units(units: f64) -> Self {
///         Cubits { forearms: units }
///     }
/// }
///
/// // Invoke the macro to automatically implement Add, Sub, etc...
/// implement_measurement! { Cubits }
///
/// // The main function here is only included to make doc tests compile.
/// // You should't need it in your own code.
/// fn main() { }
/// ```
#[macro_use]
pub mod measurement;

`char保留用于删除列名。

答案 2 :(得分:1)

你想说的如下。用singlequote替换那些背景

String sql = "INSERT INTO players (player_uuid , name , age , gender , bios) 
VALUES ( 'UUID' ,'TestName' , 8 , 'Female' , 'TestBios')";

答案 3 :(得分:0)

使用此代码的代码。发生错误是因为您没有在SQL查询中使用单个cote作为字符串值。实际上,最好的方法是使用PreparedStatement。

 String sql = "INSERT INTO players (player_uuid , name , age , gender , bios) "
            + "VALUES ( 'UUID' , " + " 'TestName' , 8 , 'Female' , 'TestBios')";