Your first effort does not work because QSqlQuery returns a bool indicating the success, not the results of the query. Then when you go to output the "result" you add an integer value to a const char* (i.e. your string literal), which adjusts the value of the pointer and has the effect of "removing" characters from the front of the string not magically converting the integer to a string and appending it.
Your second attempt will only set the int variable if the query is both successful and it returns a row: one does not necessarily follow from the other.
Try looking at QSqlQuery::lastError() if (when) exec() and next() fail.
Bookmarks