c ++ mysql查询字符串解析引号和变量(c++ mysql query string parsing with quotes and variables)
我试图在不使用c ++中的mysql连接器类的情况下更新mysql数据库。 但我无法弄清楚如何将长整数添加到查询将接受的字符串。
#define CONVERSION 16 long myCounter = 0 ; long gallons = 0; gallons = myCounter / CONVERSION; string sql = ("UPDATE utilityroom.Sprinklers SET " "gallons='@gallons' WHERE label='PUMP';"); query_state = mysql_query(connection, sql); result = mysql_store_result(connection);
它一直在查询中的第二个参数失败。
错误:无法将'std :: string {aka std :: basic_string}'转换为'const char *'以将参数'2'转换为'int mysql_query(MYSQL *,const char *)'
编辑:
我讨厌c ++处理字符串的方式。 我不明白为什么每个字符串都必须转换为类元素!
以下是我改变它以使其工作:
#include <sstream> ostringstream strstr; strstr << "UPDATE utilityroom.Sprinklers SET gallons='" << gallons << "' WHERE label='PUMP';"; string sql = strstr.str(); query_state = mysql_query(connection, sql.c_str());
I am trying to update a mysql database without using mysql connector class in c++. But I can't figure out how to add a long integer to a string that the query will accept.
#define CONVERSION 16 long myCounter = 0 ; long gallons = 0; gallons = myCounter / CONVERSION; string sql = ("UPDATE utilityroom.Sprinklers SET " "gallons='@gallons' WHERE label='PUMP';"); query_state = mysql_query(connection, sql); result = mysql_store_result(connection);
It keeps failing at the 2nd argument in the query.
error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘2’ to ‘int mysql_query(MYSQL*, const char*)’
EDIT:
I hate the way c++ handles strings. I don't understand why every string has to be converted to a class element!
Here is what I changed to get it working:
#include <sstream> ostringstream strstr; strstr << "UPDATE utilityroom.Sprinklers SET gallons='" << gallons << "' WHERE label='PUMP';"; string sql = strstr.str(); query_state = mysql_query(connection, sql.c_str());
原文:https://stackoverflow.com/questions/41666357
最满意答案
您可以在
SELECT
子句中对列值进行硬编码:SELECT 'box1' AS col, SUM(box1) as box1Count FROM queue UNION ALL SELECT 'box2', SUM(box2) as box2Count FROM queue UNION ALL SELECT 'box3', SUM(box3) as box3Count FROM queue
You can hard-code the column value in the
SELECT
clause:SELECT 'box1' AS col, SUM(box1) as box1Count FROM queue UNION ALL SELECT 'box2', SUM(box2) as box2Count FROM queue UNION ALL SELECT 'box3', SUM(box3) as box3Count FROM queue
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
删除mysql中的重复行(Delete duplicate rows in mysql)[2023-09-27]
您的查询有很多问题: DELETE FROM location where country in (SELECT * FROM table_name GROUP BY country, state, city having COUNT(*)>); 例如:(1)您从子查询中返回多个列,但仅比较一列。 (2)比较后你没有号码。 (3)它指两个不同的表格。 数据结构化的方式(在一行中没有唯一的ID), ... -
在MYSQL中,拥有多行或多列是否更好?(In MYSQL, is it better to have many rows or many columns? [duplicate])[2022-02-27]
显然是第二个。 所有这些属性都应该放在一个单独的列中,否则我没有必要将它们存储在RDBMS中...您也可以将它们存储在文本文件中 Obviously the Second one. All those Attributes should go in a separate column else there is no point i storing them in RDBMS ... you can rather store them in text file as well -
我想识别它们 你不能。 您只能通过唯一(或主键)来识别行。 但是,如果你有这个,就不会有任何确切的重复。 但是 - 如果您真的不想列出所有列,则可以创建该表的临时副本并添加AUTO_INCREMENT PRIMARY KEY。 然后, NATURAL JOIN , GROUP BY和COUNT将返回重复的行: drop table if exists my_duplicates; create table my_duplicates (c1 int, c2 int, c3 int); insert into ...
-
防止所有非唯一列的重复行(仅适用于MySQL)?(Prevent duplicate rows with all non-unique columns (only with MySQL)?)[2022-06-01]
将user_id和role_id列定义为表的主键。 CREATE TABLE users_roles ( user_id INT(100) NOT NULL, role_id INT(100) NOT NULL, PRIMARY KEY(user_id, role_id) FOREIGN KEY (user_id) REFERENCES users(user_id), FOREIGN KEY (role_id) REFERENCES roles(role_id) ) ENGINE = I ... -
尝试以下查询oracle select emp_no, rtrim (xmlagg (xmlelement (e, emp_name || ',')).extract ('//text()'), ',') enames from emp group by emp_no ; 对于sql server试试 SELECT * FROM ( SELECT emp_name FROM tblemployee ) as s PIVOT ( min ...
-
根据MySql中的两列删除重复记录[重复](Delete duplicate records based on two columns in MySql [duplicate])[2023-02-09]
用这个 :- DELETE FROM corporate WHERE corporate_sed_id IN( SELECT * FROM ( SELECT MIN(corporate_sed_id) FROM corporate GROUP BY corporate_id, category_id ... -
您可以在SELECT子句中对列值进行硬编码: SELECT 'box1' AS col, SUM(box1) as box1Count FROM queue UNION ALL SELECT 'box2', SUM(box2) as box2Count FROM queue UNION ALL SELECT 'box3', SUM(box3) as box3Count FROM queue You can hard-code the column value in the SELECT clause: S ...
-
将输入的data.frame转换为列表,并根据列的常用列名将列拆分为组。 然后取消列出每组列以在每个组中生成单个列并转换回data.frame。 (如果DF有多行,这也可以。) as.data.frame(lapply(split(as.list(DF), names(DF)), unlist)) 赠送: Rating Title Year 1 6.7 Movie1 1997 2 8.2 Movie2 1987 3 7.1 Movie3 2009 注意:我们假设了这个输入: Li ...
-
嘿试试这个用于表的转换: SQLFIDDLE set @sql = null; select group_concat(distinct concat( 'max(case when PieceType = ''', PieceType, ''' then Qty end) AS ', concat(PieceType) ) ) into @sql from customer c LEFT JOIN job_new jn ...