gpt4 book ai didi

c++ - 警告 : missing terminating "character

转载 作者:行者123 更新时间:2023-11-30 00:42:41 24 4
gpt4 key购买 nike

我正在制作一个连接到 SQL 数据库的 C++ 程序。这个查询真的很长,可能不是解决它的最佳方法,但这不是重点。此错误是指此代码中的第一个 ",就在 SELECT 之前。考虑到结尾的 " 位于函数结束。我假设我需要在某个地方使用一些转义字符,但我把它们放在任何地方似乎都没有做任何事情。

res = stmt->executeQuery("SELECT customers.customerNumber, customers.customerName, customers.phone, customers.creditLimit, orders.orderNumber, orders.orderDate, orders.status, products.productName, orderdetails.quantityOrdered, orde
rdetails.priceEach, employees.firstName, employees.lastName, employees.email
AS returnedInfo
FROM customers
JOIN orders ON customers.customerNumber=orders.customerNumber
JOIN orderdetails ON orders.orderNumber = orderdetails.orderNumber
JOIN products ON orderdetails.productCode = products.productCode
JOIN employees ON customers.salesRepEmployeeNumber = employees.employeeNumber
WHERE customers.customerNumber = \'103\';");

最佳答案

文字字符串需要在行结束之前结束。您可以通过使用预处理器行继续来解决这个问题,如

res = stmt->executeQuery("SELECT customers.customerNumber, customers.customerName, customers.phone, customers.creditLimit, orders.orderNumber, orders.orderDate, orders.status, products.productName, \orderdetails.quantityOrdered, orderdetails.priceEach, employees.firstName, employees.lastName, employees.email \
AS returnedInfo \
FROM customers \
JOIN orders ON customers.customerNumber=orders.customerNumber \
JOIN orderdetails ON orders.orderNumber = orderdetails.orderNumber \
JOIN products ON orderdetails.productCode = products.productCode \
JOIN employees ON customers.salesRepEmployeeNumber = employees.employeeNumber \
WHERE customers.customerNumber = '103';");

在连接相邻字符串文字的地方使用编译器字符串文字连接:

res = stmt->executeQuery("SELECT customers.customerNumber, customers.customerName, customers.phone, customers.creditLimit, orders.orderNumber, orders.orderDate, orders.status, products.productName, orderdetails.quantityOrdered, orderdetails.priceEach, employees.firstName, employees.lastName, employees.email "
"AS returnedInfo "
"FROM customers "
"JOIN orders ON customers.customerNumber=orders.customerNumber "
"JOIN orderdetails ON orders.orderNumber = orderdetails.orderNumber "
"JOIN products ON orderdetails.productCode = products.productCode "
"JOIN employees ON customers.salesRepEmployeeNumber = employees.employeeNumber "
"WHERE customers.customerNumber = '103';");

请注意,这两者实际上都将创建单行。如果您想将它作为多行传递给函数,您需要自己在每个“行”的末尾添加换行符 \n 。或者使用 the answer by druckermanly 中提到的原始字符串文字.

旁注:您可能已经注意到,我没有转义单引号“字符串”。在双引号字符串中,您不必转义单引号。

关于c++ - 警告 : missing terminating "character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58683239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com