插入 mysql table 时出现异常
Exception while inserting mysql table
我已经从下面的脚本中生成了 mysql table
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 ;
问题是我无法像这样向 table 插入任何值
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
这样做的正确方法是什么。
从数据库名称和 table 名称中删除单引号。
sql = "INSERT INTO test.Record values(?)";
PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, URL);
stmt.execute();
您可以使用反引号 (`) 代替单引号。它会起作用。
我已经从下面的脚本中生成了 mysql table
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 ;
问题是我无法像这样向 table 插入任何值
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
这样做的正确方法是什么。
从数据库名称和 table 名称中删除单引号。
sql = "INSERT INTO test.Record values(?)";
PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, URL);
stmt.execute();
您可以使用反引号 (`) 代替单引号。它会起作用。