1

I took sqlconnect from a C++ sample example from here: SQL connect from C++. I want to insert data into MySQL table from C++. I am just trying to run first sample example to get rid of it. Please suggest what should I take care of?

#include <stdlib.h>
#include <iostream>

/*
  Include the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

using namespace std;

int main(void)
{
    cout << endl;
    cout << "Let's have MySQL count from 10 to 1..." << endl;

    try {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;
        sql::PreparedStatement *pstmt;

        /* Create a connection */
        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
        /* Connect to the MySQL test database */
        con->setSchema("test");

        stmt = con->createStatement();
        stmt->execute("DROP TABLE IF EXISTS test");
        stmt->execute("CREATE TABLE test(id INT)");
        delete stmt;

        /* '?' is the supported placeholder syntax */
        pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
        for (int i = 1; i <= 10; i++) {
            pstmt->setInt(1, i);
            pstmt->executeUpdate();
        }
        delete pstmt;

        /* Select in ascending order */
        pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
        res = pstmt->executeQuery();

        /* Fetch in reverse = descending order! */
        res->afterLast();
        while (res->previous())
          cout << "\t... MySQL counts: " << res->getInt("id") << endl;
        delete res;

        delete pstmt;
        delete con;

    } catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " »
             << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << »
                " )" << endl;
    }

    cout << endl;

    return EXIT_SUCCESS;
}

Everything is fine. Why does it give the following compilation errors?

temp.cpp:65:3: error: stray ‘\302’ in program
temp.cpp:65:3: error: stray ‘\273’ in program

temp.cpp:69:3: error: stray ‘\302’ in program
temp.cpp:69:3: error: stray ‘\273’ in program

I saw a similar question on Stack Overflow, but still it does not get solved!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Did you look at lines 65 and 69, as the error messages indicated? In particular, did you spot the » character at the end of those lines? – Mike Seymour Sep 05 '13 at 09:45
  • Of the common occurring Unicode 'characters', there are only the two "»"s (near "`on line`" and "`e.getSQLState`"). They can be searched for by the regular expression `\x{00BB}` in any modern text editor or IDE (note: The notation is different in Visual Studio Code (and probably others): `\u00BB` (instead of `\x{00BB}`)). – Peter Mortensen May 03 '23 at 04:08
  • Also, 302 273 (octal) → 0xC2 0xBB (hexadecimal) → UTF-8 sequence for Unicode code point U+00BB ([RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=128)). – Peter Mortensen May 03 '23 at 04:11
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 03 '23 at 04:14
  • A web page is revealed which makes copying from a web page the probable cause, but the link is now broken (*"Page Not Found"*). – Peter Mortensen May 03 '23 at 04:16
  • Another Stack Overflow question involving RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK is *[Error "error: stray '\302' "in program for this Linux kernel code](https://stackoverflow.com/questions/30979387/)* (though the actual source containing it is never revealed, only the errors \302 \273). – Peter Mortensen May 03 '23 at 04:29

2 Answers2

3

Somewhere on line 65 and 69 you have some odd characters. They might be invisible characters, so in the general case when you encounter this error, just delete the entire line, and type it again.

In this case you have some odd characters here:

cout << "(" << __FUNCTION__ << ") on line " »
                                            ^^
                                     What's this?

You should probably delete that, and same a few lines further down.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nos
  • 223,662
  • 58
  • 417
  • 506
1

Which 'similar thread'? There was one recently, and the problem was described to be caused by copy-pasting code from a website that translated straight quotes and dashes into curly quotes and en-dashes. These, in turn, were transliterated using UTF-8, and indeed:

302 (octal) is "Â" -- 0xC2 273 is "»" -- 0xBB

This yields a full valid UTF8 code "0xC2BB", which happens to be the character "»" again. Now look at line 65 in your code -- there it is, just as the error message said.

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • That is an interesting origin theory, but how is "Â" a curly quote? Are [code pages](https://en.wikipedia.org/wiki/Code_page) involved (or the equivalent)? Which one? And straight quotes and dashes don't seem to be near each other in this case. – Peter Mortensen May 03 '23 at 04:38
  • A common code page is [Windows-1250](https://en.wikipedia.org/wiki/Windows-1250). On it, "«" is 0xAB (octal \253) and "»" is 0xBB (octal \273). \273 (octal), 0xC2, is "Â" on that code page. The same for [ISO/IEC 8859-2](https://en.wikipedia.org/wiki/ISO/IEC_8859-2#Code_page_layout). – Peter Mortensen May 03 '23 at 12:34
  • OK, the OP has left the building: *"Last seen more than 2 years ago"*. Perhaps somebody else can chime it? – Peter Mortensen May 03 '23 at 12:39