1

私はここから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で同様のスレッドを見ましたが、それでも解決しません!

4

2 に答える 2

1

どの「類似スレッド」?最近 1 つあり、この問題は、ストレート クォートとダッシュをカーリー クォートとエン ダッシュに変換する Web サイトからコードをコピーして貼り付けたことが原因であると説明されていました。これらは、UTF-8 を使用して文字変換されたものであり、実際には次のとおりです。

302 (8 進数) は "Â" -- 0xC2 273 は "»" -- 0xBB

これにより、完全に有効な UTF8 コード "0xC2BB" が得られますが、これはたまたま文字 "»" です。コードの 65 行目を見てください。エラー メッセージにあるとおりです。

于 2013-09-05T09:46:14.587 に答える