我如何通过C ++连接到我的SQL数据库?

问题描述 投票:1回答:2

我见过类似的问题,但是大多数问题都基于PHP,有一个问题基于C,但是唯一的答案是我已经完成的SQLConnect()文档。https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlconnect-function?redirectedfrom=MSDN&view=sql-server-ver15

似乎在线上内容很少,我也是初学者,所以请原谅任何愚蠢的问题。我使用MYSQL Workbench设置了SQL数据库,它位于我的localhost:3306上。我能够将其成功连接到ODBC数据源管理程序。

所以,我相信我使用的是正确的函数,并且设置了正确的SQL后端,我只是不知道从哪里可以得到。

最终目标是能够使用该程序将记录“插入”到我的数据库中,也可以从中“选择”。

我在做什么错了,或者我想念什么?

    # include <stdio.h>
    # include <stdlib.h>
    # include <sql.h>
    # include <sqlext.h>
    # include <cstdio>
    # include <cstdint>
    using namespace std;

    int main(){
        SQLHENV henv = NULL;
        SQLHDBC hdbc = NULL;

        /* Initialize the ODBC environment handle. */
        SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);

        /* Set the ODBC version to version 3 (the highest version) */
        SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
            (void*)SQL_OV_ODBC3, 0);

        /* Allocate the connection handle. */
        SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

        SQLConnectW(hdbc, (SQLWCHAR*)"localhost", (SQLSMALLINT) 9, 
                    (SQLWCHAR*)"root", (SQLSMALLINT)4, 
                    (SQLWCHAR*)"password", (SQLSMALLINT)8);

        return(0);
    }
c++ mysql database-connection
2个回答
0
投票

我有一个类似的任务,我必须使用C ++和MYSQL。绝对不可能从头开始,但是希望这个例子会有所帮助。我的老师用它来帮助我们开始作业。如果可能,我可以尝试并提供更多帮助!该存储库位于https://github.com/ChrisTroupe/Assignment4_DB/tree/master,请查看“ example_code.cpp”。祝你好运!


0
投票

我已在代码内部添加了注释,这将帮助您轻松理解代码。积分:Link

/* Includes Standard C++  */

#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>

using namespace std;

int main(void)
{
  cout << endl;
  cout << "Running 'SELECT 'Successfull' »
           AS _message'..." << endl;

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

  /* 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();
  res = stmt->executeQuery("SELECT 'Successfull' AS _message"); // replace with your statement
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column fata by numeric offset, 1 is the first column */
    cout << res->getString(1) << endl;
  }
  delete res;
  delete stmt;
  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;
}
© www.soinside.com 2019 - 2024. All rights reserved.