C를 사용하여 MySQL 데이터베이스에 연결하고 쿼리를 실행하는 방법은 무엇입니까?

Barbara Streisand
풀어 주다: 2024-10-27 00:16:31
원래의
336명이 탐색했습니다.

How to Connect to a MySQL Database and Execute Queries Using C  ?

C를 사용하여 MySQL 데이터베이스에 연결하는 방법

웹 사이트와 MySQL 데이터베이스 간의 연결을 설정하고 다음에서 선택 쿼리를 실행하려면 테이블 행을 사용하려면 적절한 C 라이브러리를 활용해야 합니다. 이를 달성하는 방법은 다음과 같습니다.

다음 라이브러리를 가져옵니다.

<code class="cpp">#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;</code>
로그인 후 복사

C 프로그램 설정:

<code class="cpp">int main(void)
{
  // Initialize connection details
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  try {
    // Establish connection to MySQL
    driver = get_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
    con->setSchema("test"); // Replace "test" with your database name

    // Create statement object
    stmt = con->createStatement();

    // Execute select query
    res = stmt->executeQuery("SELECT 'Hello World!' AS _message");

    // Display query results
    while (res->next()) {
      cout << "\t... MySQL replies: " << res->getString("_message") << endl;
      cout << "\t... MySQL says it again: " << res->getString(1) << endl;
    }

    // Clean up resources
    delete res;
    delete stmt;
    delete con;
  }
  catch (sql::SQLException &amp;e) {
    // Handle database exception
    cout << "# ERR: SQLException in " << __FILE__ << endl;
    cout << "# ERR: " << e.what() << endl;
  }

  return EXIT_SUCCESS;
}</code>
로그인 후 복사

라이브러리를 활용하고 다음 단계를 따릅니다. 위에 설명된 대로 C를 사용하여 MySQL 데이터베이스에 성공적으로 연결하고 테이블 행에서 선택 쿼리를 실행할 수 있습니다.

위 내용은 C를 사용하여 MySQL 데이터베이스에 연결하고 쿼리를 실행하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!