警告:缺少终止 " 字符

warning: missing terminating " character

我正在制作一个连接到 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\';");

你想要一个 raw string literal(在 C++11 中引入)。问题是,默认情况下,您不能在字符串中放置实际的换行符(而不是 \n),但可以使用原始字符串文字。

如果您不使用 C++11 或更高版本,则可以使用其他技术(续行或连接顺序字符串),但如果您可以使用,这通常是表达您意图的最简洁的方法。

这是一个使用您的查询的示例:

res = stmt->executeQuery(R"SQL(
  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';
)SQL");

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

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';");

请注意,这两者都将创建实际上是 行的内容。如果你想将它作为多行传递给函数,你需要自己在每个 "line" 的末尾添加换行符 \n 。或者使用 .

中提到的原始字符串文字

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