Negative Zero
Negative Zero

Reputation: 205

How to read this qtstring in qsqlquery.exec

I am learning qtsql but come across this qtstring in qtsql::query.exec()

I never see this string before in C++.

Can someone explain why there are some many quotation? And how to read this? Is this one string or five strings? If there are five strings, why there aren't any separators like a comma in between? Also query.exec is expecting a const & QTstring. So if this is one string, is it same as "create table person (id integer primary key, firstname varchar(20), lastname varchar(30), age integer)"?

ret = query.exec("create table person "
              "(id integer primary key, "
              "firstname varchar(20), "
              "lastname varchar(30), "
              "age integer)");

Thanks in advance.


It doesn't compile, as I get this error

[100%] Building CXX object CMakeFiles/utest_main.dir/utest_main.cpp.o /Users/chen/GitRepo/TickDatabase/utest_main.cpp: In function ‘int main()’: /Users/chen/GitRepo/TickDatabase/utest_main.cpp:25: error: ‘firstname’ was not declared in this scope /Users/chen/GitRepo/TickDatabase/utest_main.cpp:25: error: ‘lastname’ was not declared in this scope /Users/chen/GitRepo/TickDatabase/utest_main.cpp:25: error: ‘age’ was not declared in this scope make[2]: * [CMakeFiles/utest_main.dir/utest_main.cpp.o] Error 1 make[1]: * [CMakeFiles/utest_main.dir/all] Error

But it also doesn't look like an error, since both these two sites are referring to this syntax:

http://www.developer.nokia.com/Community/Wiki/CS001505_-_Creating_a_database_table_in_Qt

http://doc.qt.nokia.com/stable/sql-cachedtable.html

Anyone got any idea?

Upvotes: 0

Views: 216

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409482

If two string literals are separated only by whitespace, then they are concatenated into one single string. It's part of the C and C++ standards if I'm not mistaken.

Edit The standard for C can be found here. The relevant part is section 6.4.5 (aptly named "String literals"), paragraph 4. I quote:

In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence.

Later in paragraph 7 they have an example:

EXAMPLE This pair of adjacent character string literals

"\x12" "3"

produces a single character string literal containing the two characters whose values are '\x12' and '3', because escape sequences are converted into single members of the execution character set just prior to adjacent string literal concatenation.

There should be similar text in the C++ standard, but I don't have it yet so can't say which section.

Edit 2 Looking at the errors in the question, I would say that there is a missing or extra double-quote " somewhere in the real source.

Upvotes: 1

Alexander Larin
Alexander Larin

Reputation: 81

This is a single string that means "create table person (id integer primary key, firstname varchar(20), lastname varchar(30), age integer)". But you can see that it is too long to get into one line. Therefore, it is splitted into several strings using "" (opening/quote closing quotes). In this case, you can use a line break inside the string in the code to improve its readability. If you do not insert these characters and just split the string using line breaks, your code will not compile.

Upvotes: 0

Related Questions