How to connect and execute a simple query to SQL Server from C ++ to Linux

I assume that I have all the prerequisites for establishing a connection and querying the SQL Server , since I can do this from my Python code. And I do it like this:

 #1. Set connection using pyodbc library db = pyodbc('DRIVER=FreeTDS;SERVER='+host+';PORT='+port+';DATABASE='+ \ db_name+ ';UID='+ user+ ';PWD='+ pwd+ ';TDS_Version=7.0;ClientCharset=UTF8;') #2. List all table names in a particular database cursor = db.cursor() cursor.execute('SELECT TABLE_NAME FROM ' + db_name + '.INFORMATION_SCHEMA.Tables WHERE ' + \ 'TABLE_TYPE=\'BASE TABLE\'') res = cursor.fetchall() 

And I'm done. Now I want to implement the same thing using C++ . I have seen some examples of codes like this , but they look awfully awful if the five if nested together. But I need something very simple, given that perhaps all the prerequisites are fulfilled (if not, please indicate what else needs to be established).

The last thing I want to know is really how to compile this program (I usually do this with g++ ). And please don't post the only links to FreeTDS and ODBC - I already saw them. What I want at this stage is a tiny minimized example of executing the simplest query in the world (for example, I did it above using Python ).

+8
c ++ linux sql-server
source share
3 answers

This has not been tested with the freetds unixodbc driver. This is how I install unixodbc with postgres or mysql. http://k3ut0i.imtqy.com/linux/2015/08/09/odbc-setup-linux.html

The global driver and data source configuration for ODBC is located in /etc/odbc.ini and /etc/odbcinst.ini . some operating systems may have these files empty or with different names. Here are my configuration files.

Drivers, libraries in /etc/odbcinst.ini.

  [MySQL] Description = ODBC driver for mariaDB Driver = /usr/lib/libmyodbc.so Setup = /usr/lib/libmyodbc5S.so FileUsage = 1 

The pyodbc function in the python file is equivalent to the data source in /etc/odbc.ini.

  [mariadb-connector] Description = connection to test database for mariadb Driver = MySQL Database = test Server = 127.0.0.1 UserName = keutoi Trace = No Port = 3306 

The piece of code that connects and requests. I just started with your link and cleaned it.

  /** * Connect to data source named in /etc/odbc.ini file */ retcode = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)"DSN=mariadb-connector;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_COMPLETE); check_error(retcode, "connect to the data source"); //Allocate statement handle retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); check_error(retcode, "allocate a statement handle"); /** * statement to be executed. */ retcode = SQLExecDirect (hstmt, (SQLCHAR *) "select * from mytable", SQL_NTS); check_error(retcode, "execute the statement"); /** * Bind a column to a variable */ retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, szName, NAME_LEN, &cbName); check_error(retcode, "bind 1 column to the statement"); retcode = SQLBindCol(hstmt, 2, SQL_C_CHAR, szID, ID_LEN, &cbID); check_error(retcode, "bind 2 column to the statement"); /** * fetch sql hstmt untill there is no data and print */ for (int i=0 ; ; i++) { retcode = SQLFetch(hstmt); if(retcode == SQL_NO_DATA)break; else printf( "%d: %s %s %s\n", i + 1, szID, szName); } 

Full example here . Compile with g++ file.cc -lodbc

+5
source share

This seems like a pretty simple example without very small ifs.

http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

+1
source share
+1
source share

All Articles