Joel Gray
Joel Gray

Reputation: 319

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. https://learn.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. I was able to connect it to the ODBC Data Source Admin program successfully.

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.

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

Upvotes: 1

Views: 2149

Answers (1)

Dinesh kapri
Dinesh kapri

Reputation: 68

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

Upvotes: 1

Related Questions