Reputation: 193
I'm making a C++ program that connects to an SQL database. This query is really long and probably not the best way to go about it, but that's besides the point. This error is referring to the first "
in this code, right before SELECT
. Not sure what the problem is considering the ending "
is at the end of the function. I'm assuming that I need to use some escape characters somewhere but nowhere that I put them seem to do anything.
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\';");
Upvotes: 1
Views: 2042
Reputation: 2741
You want a raw string literal (introduced in C++11). The problem is that, by default, you can't put an actual newline (instead of \n
) in your string, but you can with raw string literals.
If you're not using C++11 or later, you can use other techniques (line continuations, or concatenated sequential strings) but this is often the cleanest way to represent your intent if it's available to you.
Here's an example using your query:
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");
Upvotes: 3
Reputation: 409166
Literal strings needs to end before the line ends. You can work around that by using the preprocessor line-continuation, as in
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';");
Of use the compilers string-literal concatenation where it concatenates adjacend string literals:
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';");
Note that both of these will create what is in reality a single line. If you want to pass it to the function as multiple lines you need to add the newline \n
at the end of each "line" yourself. Or use raw string literals as mentioned in the answer by druckermanly.
On a side-note: As you might have noticed, I don't escape the single quoted "string". In double-quoted strings you don't have to escape the single-quote.
Upvotes: 3