我正在尝试使用以下方式将FLOAT类型的值插入表中(正在使用C++连接器)

res = stmt->executeQuery("INSERT INTO `ticker_info`(`a1`,`a2`,`a3`,`b1`,`b2`,`b3`,`c1`,`c2`,`v1`,`v2`,`p1`,`p2`,`t1`,`t2`,`l1`,`l2`,`h1`,`h2`,`o1`) VALUES ('" + a1 + "','" + a2 + "','" + a3 + "','" + b1 + "','" + b2 + "','" + b3 + "','" + c1 + "','" + c2 + "','" + v1 + "','" + v2 + "','" + p1 + "','" + p2 + "','" + t1 + "','" + t2 + "','" + l1 + "','" + l2 + "','" + h1 + "','" + h2 + "','" + o1 + "' " );
该表如下创建
CREATE TABLE `ac`.`ticker_info` ( `a1` FLOAT NOT NULL , `a2` INT NOT NULL , `a3` FLOAT NOT NULL , `b1` FLOAT NOT NULL , `b2` INT NOT NULL , `b3` FLOAT NOT NULL , `c1` FLOAT NOT NULL , `c2` FLOAT NOT NULL , `v1` FLOAT NOT NULL , `v2` FLOAT NOT NULL , `p1` FLOAT NO
T NULL , `p2` FLOAT NOT NULL , `t1` INT NOT NULL , `t2` INT NOT NULL , `l1` FLOAT NOT NULL , `l2` FLOAT NOT NULL , `h1` FLOAT NOT NULL , `h2` FLOAT NOT NULL , `o1` FLOAT NOT NULL ) ENGINE = InnoDB;
但是,这样做失败了
ac.cpp: In function ‘int main(int, char**)’:
ac.cpp:445:161: error: invalid operands of types ‘const char [132]’ and ‘float’ to binary ‘operator+’
  445 |  res = stmt->executeQuery("INSERT INTO `ticker_info`(`a1`,`a2`,`a3`,`b1`,`b2`,`b3`,`c1`,`c2`,`v1`,`v2`,`p1`,`p2`,`t1`,`t2`,`l1`,`l2`,`h1`,`h2`,`o1`) VALUES ('" + a1 + "','" + a2)  + "','" + a3 + "','" + b1 + "','" + b2 + "','" + b3 + "','" + c1 + "','" + c2 + "','" + v1 + "','" + v2 + "','" + p1 + "','" + p2 + "','" + t1 + "','" + t2 + "','" + l1 + "','" + l2 + "','" + h1 + "','" + h2 + "','" + o1 + "' " );
      |                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~
      |                           |                                                                                                                                       |
      |                           const char [132]                                                                                                                        float
我在这里做错了什么?我真的需要对浮点值进行字符串化处理吗?我希望我可以按原样提供类型。我搜索了一段时间以寻找解决方案,但看起来我很愚蠢(可能),或者以前没有人遇到这个问题(不太可能)。
不幸的是,文档对我也是如此。

最佳答案

根据@lucumt建议,准备带有参数标记的语句并绑定(bind)值可解决此问题。

关于c++ - 将FLOAT值插入mysql表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64185490/

10-08 21:01