tags:

views:

37

answers:

1

Hello,
Two [hopefully] quick questions regarding C++/Qt. Is the following proper for writing a string on multiple lines?

QString strQuery;
strQuery="\
         CREATE TABLE foo\
             (bar integer primary key,\
             baz varchar(20))";

I believe this is right, but in Qt Creator it doesn't highlight as though it is one big string.

Secondly, will QSqlQuery.exec() run multiple queries in a single execution or does each query need to be run through exec()? For example, I'm trying something like:

QSqlQuery query;
QString strQuery;
strQuery="\
         CREATE TABLE foo \
             (bar integer primary key,\
             baz varchar(10));\
        CREATE TABLE herp\
             (de integer primary key, \
             derp varchar(10))";
query.exec(strQuery);

From what I can see, only that first table is being created. I don't know if this is related to my multiline string, to my database type (SQLite), or just QSqlQuery in general.

Thanks for the help!

+1  A: 

I think that will be the right way:

QString strQuery;
strQuery="CREATE TABLE foo " \
         "(bar integer primary key, " \
         "baz varchar(20));";
// with this style `strQuery` will be single line, like "CREATE TABLE foo (bar integer primary key, baz varchar(20));"

QSqlQuery query;

QString strQuery;
strQuery="CREATE TABLE foo " \
         "(bar integer primary key, " \
         "baz varchar(10));"
query.exec(strQuery);

strQuery="CREATE TABLE herp " \
         "(de integer primary key, " \
         "derp varchar(10))";
query.exec(strQuery);
mosg
No need to escape the linefeeds with \. It's just whitespace and the C/C++ preprocessor automatically concatenates "string" "literals" with only whitespace in between.
laalto
I'd really like to be able to run multiple queries without having to do it as you've shown. SQLite seems to be capable of running multiple queries in a single execution, but I can't seem to get it to work from QSqlQuery::exec. I'll hold out a bit on this and hope someone can explain what I'm looking for; otherwise I'll mark your answer correct.
Airjoe
@laalto: I know that
mosg