c++ - 错误 : stray ‘\302’ in program error while connecting mysql from c++

标签 c++ mysql

我从这里的 C++ 示例中获取了 sqlconnect:sql connect from c++ .我想从 C++ 中将数据插入到 mysql 表中。我只是想运行第一个示例示例来摆脱它。请建议我应该注意什么?

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

/*
  Include directly 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;
}

一切正常,为什么报错:

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

我在 SO 上看到了类似的帖子,但仍然没有解决!

最佳答案

在第 65 和 69 行的某处,你有一些奇怪的字符,它们可能是不可见的字符,所以在一般情况下,当你遇到这个错误时,只需删除整行,然后重新输入即可。

在这种情况下,这里有一些奇怪的字符:

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

你可能应该删除它,并在下面几行相同。

关于c++ - 错误 : stray ‘\302’ in program error while connecting mysql from c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18632791/

相关文章:

c++ - 这个 C++ 程序的格式有什么问题?

mysql - 使用索引优化带有 ORDER BY 的 SELECT 语句

php - 选择与下拉列表中显示的值不同的值。 (PHP 和 MySQL)

c++ - 如何设置默认参数以防用户未输入另一个参数

c++ - 使用 boost 图进行图像分割

使用 C++/CLR dll 时 C# 应用程序崩溃

mysql - 查询以从表中选择间隔

mysql:如果不存在则插入记录,否则返回记录的id

mysql - eloquent,用外表数据检索用户数据

c++ - 为什么断言宏的定义在发布版本中不能只是 `#define assert(expression) 0` ?