I have a pretty long sqlite query:
const char *sql_query = "SELECT statuses.word_id FROM lang1_words, statuses WHERE statuses.word_id = lang1_words.word_id ORDER BY lang1_words.word ASC";
How can I break it in a number of lines to make it easier to read? If I do the following:
const char *sql_query = "SELECT word_id FROM table1, table2 WHERE table2.word_id = table1.word_id ORDER BY table1.word ASC";
I am getting an error.
Is there a way to write queries in multiple lines?
You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.
We can use string literal concatenation. Multiple string literals in a row are joined together: char* my_str = "Here is the first line." "Here is the second line."; But wait!
Use the line-continuation character, which is an underscore ( _ ), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.
There are two ways to split strings over multiple lines:
Each string on its own line. Works only with strings:
Plain C:
char *my_string = "Line 1 " "Line 2";
Objective-C:
NSString *my_string = @"Line1 " "Line2"; // the second @ is optional
Using \
- can be used for any expression:
Plain C:
char *my_string = "Line 1 \ Line 2";
Objective-C:
NSString *my_string = @"Line1 \ Line2";
The first approach is better, because there isn't a lot of whitespace included. For a SQL query however, both are possible.
NOTE: With a #define
, you have to add an extra \
to concatenate the two strings:
Plain C:
#define kMyString "Line 1"\ "Line 2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With