Linux使用说明
安装unix ODBC
yum -y install unixODBC unixODBC-devel
解压UDB-TX ODBC包
unzip unvdb_odbc_centos_amd64.zip
配置ODBC驱动管理器
vi /etc/odbcinst.ini
添加如下内容
[UNVDB]
Description = ODBC for UDB-TX
Driver = /usr/unvdb_odbc_x86_64/unvdbodbcw.so
Setup = /usr/lib64/libodbcpsqIS.so
Driver64 = /usr/unvdb_odbc_x86_64/unvdbodbw.so
Setup64 = /usr/lib64/libodbcpsqIS.so
FileUsage = 1
配置数据源
vi /etc/odbc.ini
添加如下内容
[dsn_unvdb]
Description = test unvdb
Driver = UNVDB
Database = unvdb
Servername = localhost
UserName = unvdb
Password = 0202
Port = 5678
ReadOnly = 0
ConnSettings = set client_encoding to UTF8
验证连接
执行isqldsn_unvdb,验证是否能正常执行SQL语句
+-----------------------+ | Connected! | | sql-statement | | help [tablename] | | quit | +-----------------------+ SQL> select * from tt; +--------+ | id | +--------+ | 1 | | 2 | +--------+ SQLRowCount returns 2 2 rows fetched SQL>
编程示例
python:
import pyodbc # Set up connection parameters conn_str = ( "DRIVER={UNVDB};" "DATABASE=unvdb;" "UID=unvdb;" "PWD=0202;" "SERVER=localhost;" "PORT=5678;" ) # Connect to the database conn = pyodbc.connect(conn_str) # Create a cursor object cursor = conn.cursor() # Execute a SQL query cursor.execute("SELECT id from tt;") # Fetch the results results = cursor.fetchall() # Print the results for row in results: print(row) # Close the cursor and connection cursor.close() conn.close()
c语言:
#include <stdio.h> #include <stdlib.h> #include <sql.h> #include <sqlext.h> int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR *dsn = (SQLCHAR *)"dsn_unvdb"; SQLCHAR *user = (SQLCHAR *)"unvdb"; SQLCHAR *pass = (SQLCHAR *)"0202"; SQLCHAR *query = (SQLCHAR *)"SELECT * FROM tt"; // Allocate an environment handle ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { printf("Error allocating environment handle\n"); return 1; } // Set the ODBC version to 3.0 ret = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { printf("Error setting environment attribute\n"); SQLFreeHandle(SQL_HANDLE_ENV, env); return 1; } // Allocate a connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { printf("Error allocating connection handle\n"); SQLFreeHandle(SQL_HANDLE_ENV, env); return 1; } // Connect to the data source ret = SQLConnect(dbc, dsn, SQL_NTS, user, SQL_NTS, pass, SQL_NTS); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { printf("Error connecting to data source\n"); SQLFreeHandle(SQL_HANDLE_DBC, dbc); SQLFreeHandle(SQL_HANDLE_ENV, env); return 1; } // Allocate a statement handle ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { printf("Error allocating statement handle\n"); SQLDisconnect(dbc); SQLFreeHandle(SQL_HANDLE_DBC, dbc); SQLFreeHandle(SQL_HANDLE_ENV, env); return 1; } // Execute the query ret = SQLExecDirect(stmt, query, SQL_NTS); if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { printf("Error executing query\n"); SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLDisconnect(dbc); SQLFreeHandle(SQL_HANDLE_DBC, dbc); SQLFreeHandle(SQL_HANDLE_ENV, env); return 1; } // Fetch the results while (SQLFetch(stmt) == SQL_SUCCESS) { SQLCHAR col[256]; SQLLEN len; // Retrieve data from each column SQLGetData(stmt, 1, SQL_C_CHAR, col, sizeof(col), &len); // Print the data printf("%s\n", col); } // Free the statement handle SQLFreeHandle(SQL_HANDLE_STMT, stmt); // Disconnect from the data source SQLDisconnect(dbc); // Free the connection handle SQLFreeHandle(SQL_HANDLE_DBC, dbc); // Free the environment handle SQLFreeHandle(SQL_HANDLE_ENV, env); return 0; }