插入mysql表时出现异常

时间:2015-01-16 13:50:28

标签: java mysql sql insert

我已经从下面的脚本生成了一个mysql表

CREATE TABLE IF NOT EXISTS `Record` (
  `RecordID` INT(11) NOT NULL AUTO_INCREMENT,
  `URL` text NOT NULL,
  PRIMARY KEY (`RecordID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

问题是我无法像这样向表中插入任何值

sql = "INSERT INTO  test.'Record' " + "('URL') VALUES " + "(?);";

以这种方式

    sql = "INSERT INTO  'test'.'Record' values(?)";
PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1, URL);
            stmt.execute();

这是异常消息

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 ''test'.'Record' values

这样做的正确方法是什么。

1 个答案:

答案 0 :(得分:0)

从数据库名称和表名中删除单引号。

sql = "INSERT INTO test.Record values(?)";
PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1, URL);
            stmt.execute();

您可以使用反引号(`)来代替单引号。它会起作用。