简体   繁体   English

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

[英]How can I connect to my SQL Database through C++?

I've seen some similar questions but most are based around PHP, there was one based around C but the only answer was go to the SQLConnect() docs, which I've already done.我见过一些类似的问题,但大多数都是基于 PHP,有一个基于 C 但唯一的答案是 go 到SQLConnect()已经完成的文档。 https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlconnect-function?redirectedfrom=MSDN&view=sql-server-ver15 https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlconnect-function?redirectedfrom=MSDN&view=sql-server-ver15

Seems to be little content online, also I'm a beginner so excuse any silly issues.网上的内容似乎很少,而且我是初学者,所以请原谅任何愚蠢的问题。 I set up the SQL database using the MYSQL Workbench and it's sitting on my localhost:3306.我使用 MYSQL 工作台设置了 SQL 数据库,它位于我的 localhost:3306 上。 I was able to connect it to the ODBC Data Source Admin program successfully.我能够成功地将它连接到 ODBC 数据源管理程序。

So I believe I'm using the right function and have the right SQL back end set up, I just don't know where to go from here.所以我相信我使用的是正确的 function 并设置了正确的 SQL 后端,我只是不知道 go 从这里到哪里。

The end goal is to be able to 'insert' records into my database with the program and also 'select' from it too.最终目标是能够使用程序将记录“插入”到我的数据库中,并从中“选择”。

What am I doing wrong, or what am I missing?我做错了什么,或者我错过了什么?

    # 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);
    }

I have added the comments inside the code, which will help you to understand the code easily.我在代码中添加了注释,这将有助于您轻松理解代码。 Credits: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM