当我看到odbcinst -j
它显示
unixODBC 2.2.14 DRIVERS............: /etc/unixODBC/odbcinst.ini SYSTEM DATA SOURCES: /etc/unixODBC/odbc.ini FILE DATA SOURCES..: /etc/unixODBC/ODBCDataSources USER DATA SOURCES..: /etc/unixODBC/odbc.ini SQLULEN Size.......: 8 SQLLEN Size........: 8 SQLSETPOSIROW Size.: 8
但是没有位置/etc/unixODBC/odbcinst.ini
.实际位置是/etc/odbcinst.ini
这样我需要改变位置.我该怎么做?
我正试图在脚本下面运行
import pyodbc cnxn = pyodbc.connect('DRIVER={SQLServer};SERVER=10.10.10.1;DATABASE=ABC;UID=username;PWD=password') cursor = cnxn.cursor()
但它显示以下错误
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
我添加了odbc.in和odbcinst.ini文件信息如下
cat odbc.ini [SQLServer] Description = ODBC for MSSQL Driver = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so Servername = Database = UID = Port = 1433 cat /etc/odbcinst.ini [SQLServer] Description = ODBC for MSSQL Driver = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so Setup = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so UsageCount = 1 FileUsage = 1
我希望问题在于,odbcinst -j
当我显示它时显示错误的路径.我不知道该怎么解决.
看起来"默认"路径未正确设置.
我很确定你已经在不同的地方提到了它,但我想提醒你应该设置正确的环境变量,如下面的链接所述:
http
://www.raosoft.com/ezsurvey /help/2007/odbc_in_unix.html http://gemfirexd.docs.pivotal.io/1.3.0/userguide/developers_guide/topics/odbc/install_config_odbc.html
这意味着在您的情况下,您可以添加几行您.bashrc
或者.bash_profile
类似的,以便每次打开shell时程序都指向正确的位置.
那么要添加的行将是:
export ODBCINI=/etc/odbc.ini export ODBCSYSINI=/etc
看一下unixODBC-2.2.14-p2包的源代码,调用odbcinst -j
它时会在代码中经过以下分支
case 'j': PrintConfigInfo(); exit(0);
和PrintConfigInfo()
会做印刷一堆信息的工作,特别是你所看到的
void PrintConfigInfo() { char szFileName[ODBC_FILENAME_MAX+1]; char b1[ 256 ], b2[ 256 ]; printf( "unixODBC " VERSION "\n" ); *szFileName = '\0'; sprintf( szFileName, "%s/odbcinst.ini", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 )); printf( "DRIVERS............: %s\n", szFileName ); *szFileName = '\0'; _odbcinst_SystemINI( szFileName, FALSE ); printf( "SYSTEM DATA SOURCES: %s\n", szFileName ); *szFileName = '\0'; _odbcinst_FileINI( szFileName ); printf( "FILE DATA SOURCES..: %s\n", szFileName ); *szFileName = '\0'; _odbcinst_UserINI( szFileName, FALSE ); printf( "USER DATA SOURCES..: %s\n", szFileName ); printf( "SQLULEN Size.......: %d\n", sizeof( SQLULEN )); printf( "SQLLEN Size........: %d\n", sizeof( SQLLEN )); printf( "SQLSETPOSIROW Size.: %d\n", sizeof( SQLSETPOSIROW )); }
现在让我们检查一个print语句,以了解它从哪里获取路径,让我们以例如该行为例
printf( "SYSTEM DATA SOURCES: %s\n", szFileName );
其中,szFileName
由以下调用设置:
_odbcinst_SystemINI( szFileName, FALSE );
在文件中定义odbcinst/_odbcinst_SystemINI.c
:
BOOL _odbcinst_SystemINI( char *pszFileName, BOOL bVerify ) { FILE *hFile; char b1[ 256 ]; sprintf( pszFileName, "%s/odbc.ini", odbcinst_system_file_path( b1 )); if ( bVerify ) { /* try opening for read */ hFile = uo_fopen( pszFileName, "r" ); if ( hFile ) uo_fclose( hFile ); else { /* does not exist so try creating it */ hFile = uo_fopen( pszFileName, "w" ); if ( hFile ) uo_fclose( hFile ); else return FALSE; } } return TRUE; }
在哪里设置要在以下行中打印的字符串
sprintf( pszFileName, "%s/odbc.ini", odbcinst_system_file_path( b1 ));
要了解如何odbcinst_system_file_path( b1 )
设置此路径,我们会查看源并找到它
char *odbcinst_system_file_path( char *buffer ) { char *path; static char save_path[ 512 ]; static int saved = 0; if ( saved ) { return save_path; } if (( path = getenv( "ODBCSYSINI" ))) { strcpy( buffer, path ); strcpy( save_path, buffer ); saved = 1; return buffer; } #ifdef SYSTEM_FILE_PATH else { strcpy( save_path, SYSTEM_FILE_PATH ); saved = 1; return SYSTEM_FILE_PATH; } #else else { strcpy( save_path, "/etc" ); saved = 1; return "/etc"; } #endif }
你可以看到通过阅读环境变量getenv( "ODBCSYSINI" )
.与其他人相似.现在,原始代码有另一个分支,但最终使用自定义函数做类似的事情.