install and use odbc at linux

</pre>//install unixodbc<p></p><p>download  from <a target=_blank href="http://www.unixodbc.org/">点击打开链接</a></p><p>$tar zxvf unixODBC-2.3.0.tar.gz</p><p>$cd unixODBC-2.3.0</p><p>$ cmake -G "Unix Makefiles" -DWITH_UNIXODBC=1</p><p>$make</p><p>$make install</p><p></p><p>//install mysql-connector</p><p>download from <a target=_blank href="http://www.mysql.com/downloads/connector/cpp/1.1.html">点击打开链接</a></p><p>$tar  mysql-connector-odbc-5.1.11-src.tar.gz</p><p>$cd mysql-connector-odbc-5.1.11-src</p><p>$./comfigure</p><p>$make</p><p>$make install</p><p></p><p>//odbc.ini (we also can use the file $MYSQL_CONNECTOR_DIR/test/odbc.ini, for example : /home/lee/Downloads/mysql-connector-odbc-5.1.11-src/test/odbc.ini )</p><p>$vi /etc/odbc.ini</p><p></p><pre class="cpp" name="code">[myodbc]
Description     = MySQL test database
Trace       = Off
TraceFile   = stderr
Driver      = /home/lee/Downloads/mysql-connector-odbc-5.1.11-src/lib/libmyodbc5.so
SERVER      = 127.0.0.1
USER        = root
PASSWORD    = sbivfh
PORT        = 3306

DATABASE    = test

//save it use :wq

export ODBCINI=/etc/odbc.ini

//test

[root@localhost etc]# isql -v myodbc
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+

SQL> show databases;
+-----------------------------------------------------------------+
| Database                                                        |
+-----------------------------------------------------------------+
| information_schema                                              |
| mysql                                                           |
| performance_schema                                              |
| test                                                            |
+-----------------------------------------------------------------+


#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <stdio.h>

/*	error code:
	1	undefined reference to `SQLAllocHandle' , 
	You need to ask gcc to link with odbc (-lodbc)

	2	error while loading shared libraries: libodbc.so.1: cannot open shared object file: No such file or directory
	echo $LD_LIBRARY_PATH
	LD_LIBRARY_PATH=/usr/local/lib/
	echo $LD_LIBRARY_PATH 
	export LD_LIBRARY_PATH

	3	[unixODBC][Driver Manager]Data source name not found, and no default driver specified (0)
	[myodbc]
	Description     = MySQL test database
	Trace       = Off
	TraceFile   = stderr
	Driver      = /home/lee/Downloads/mysql-connector-odbc-5.1.11-src/lib/libmyodbc5.so
	SERVER      = 127.0.0.1
	USER        = root
	PASSWORD    = sbivfh
	PORT        = 3306

	DATABASE    = test
*/

/*		
	mysql> CREATE TABLE test_table(col1 INT,col2 VARCHAR(40),col3 SMALLINT,col4 TIMESTAMP);
	INSERT INTO test_table(col1,col2,col3) VALUES(1,'lee',111);
*/
SQLHENV       	V_OD_Env;    // Handle ODBC environment
long       	V_OD_erg;    // result of functions
SQLHDBC       	V_OD_hdbc;                      // Handle connection
SQLHDBC 	V_OD_hstmt; 
char       	V_OD_stat[10];    // Status SQL
SQLINTEGER     	V_OD_err,V_OD_rowanz,V_OD_id;
SQLSMALLINT     V_OD_mlen,V_OD_colanz;
char            V_OD_msg[200],V_OD_buffer[200];

int main()
{
	printf("test unix odbc\n");
	// 1. allocate Environment handle and register version 
	  V_OD_erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env);
	  if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
	  {
	    printf("Error AllocHandle\n");
	    exit(0);
	  }
	 V_OD_erg=SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); 
	  if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
	  {
	    printf("Error SetEnv\n");
	    SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
	    exit(0);
	  }

	 // 2. allocate connection handle, set timeout
	  V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc); 
	  if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
	  {
	    printf("Error AllocHDB %d\n",V_OD_erg);
	    SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
	    exit(0);
	  }
	 SQLSetConnectAttr(V_OD_hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);

	// 3. Connect to the datasource "web" 
	  V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "myodbc", SQL_NTS,
		                             (SQLCHAR*) "root", SQL_NTS,
		                             (SQLCHAR*) "sbivfh", SQL_NTS);
	  if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
	  {
	    printf("Error SQLConnect %d\n",V_OD_erg);
	    SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1,  (SQLCHAR*)V_OD_stat, &V_OD_err, (SQLCHAR*)V_OD_msg,100,&V_OD_mlen);
	    printf("%s (%d)\n",V_OD_msg,V_OD_err);
	    SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
	    exit(0);
	  }
	  printf("Connected !\n");
	  V_OD_erg=SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
	  if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
	  {
	    printf("Fehler im AllocStatement %d\n",V_OD_erg);
	    SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, (SQLCHAR*)V_OD_stat,&V_OD_err,(SQLCHAR*)V_OD_msg,100,&V_OD_mlen);
	    printf("%s (%d)\n",V_OD_msg,V_OD_err);
	    SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
	    exit(0);
	  }
	// to be continue ...
	return 0;
}
/*
	[lee@localhost project]$  g++  -g -o odbc_test odbc_test.cc   -L/usr/local/lib/  -lodbc
	[lee@localhost project]$ ./odbc_test 
	test unix odbc
	Connected !
*/

$ vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

~         
// add  a new line:
<pre class="html" name="code">-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

# service iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules: iptables-restore: line 11 failed
                                                           [FAILED]
// do not care the error!!!

 
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(sql,tcp,database,input,library)