ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linux/UnixÏÂODBCµÄ°²×°¡¢ÅäÖÃÓë±à³Ì

·¢²¼Ê±¼ä:2006-03-09 00:41:59À´Ô´:ºìÁª×÷Õß:µçÄÔ²©Ê¿
±¾ÎÄÖ÷ÒªÄÚÈÝÊǽéÉÜODBCµÄ¼òµ¥Ô­Àí£¬ÒÔ¼°ÈçºÎÔÚLinux/UnixϽøÐÐODBCµÄ°²×°¡¢ÅäÖÃÓë±à³Ì¡£

Ò»¡¢ ODBCÔ­Àí

ODBC ÊÇOpen Database Connect ¼´¿ª·ÅÊý¾Ý¿â»¥Á¬µÄ¼ò³Æ£¬ËüÊÇÓÉMicrosoft ¹«Ë¾ÓÚ1991 ÄêÌá³öµÄÒ»¸öÓÃÓÚ·ÃÎÊÊý¾Ý¿âµÄͳһ½çÃæ±ê×¼£¬ÊÇÓ¦ÓóÌÐòºÍÊý¾Ý¿âϵͳ֮¼äµÄÖмä¼þ¡£Ëüͨ¹ýʹÓÃÏàÓ¦Ó¦ÓÃƽ̨ÉϺÍËùÐèÊý¾Ý¿â¶ÔÓ¦µÄÇý¶¯³ÌÐòÓëÓ¦ÓóÌÐòµÄ½»»¥À´ÊµÏÖ¶ÔÊý¾Ý¿âµÄ²Ù×÷£¬±ÜÃâÁËÔÚÓ¦ÓóÌÐòÖÐÖ±½Óµ÷ÓÃÓëÊý¾Ý¿âÏà¹ØµÄ²Ù×÷£¬´Ó¶øÌṩÁËÊý¾Ý¿âµÄ¶ÀÁ¢ÐÔ¡£

ODBC Ö÷ÒªÓÉÇý¶¯³ÌÐòºÍÇý¶¯³ÌÐò¹ÜÀíÆ÷×é³É¡£Çý¶¯³ÌÐòÊÇÒ»¸öÓÃÒÔÖ§³ÖODBC º¯Êýµ÷ÓõÄÄ£¿é£¬Ã¿¸öÇý¶¯³ÌÐò¶ÔÓ¦ÓÚÏàÓ¦µÄÊý¾Ý¿â£¬µ±Ó¦ÓóÌÐò´Ó»ùÓÚÒ»¸öÊý¾Ý¿âϵͳÒÆÖ²µ½ÁíÒ»¸öʱ£¬Ö»Ðè¸ü¸ÄÓ¦ÓóÌÐòÖÐÓÉODBC ¹ÜÀí³ÌÐòÉ趨µÄÓëÏàÓ¦Êý¾Ý¿âϵͳ¶ÔÓ¦µÄ±ðÃû¼´¿É¡£Çý¶¯³ÌÐò¹ÜÀíÆ÷¿ÉÁ´½Óµ½ËùÓÐODBC Ó¦ÓóÌÐòÖУ¬Ëü¸ºÔð¹ÜÀíÓ¦ÓóÌÐòÖÐODBC º¯ÊýÓëDLL Öк¯ÊýµÄ°ó¶¨¡£

ODBC ʹÓòã´ÎµÄ·½·¨À´¹ÜÀíÊý¾Ý¿â£¬ÔÚÊý¾Ý¿âͨÐŽṹµÄÿһ²ã£¬¶Ô¿ÉÄܳöÏÖÒÀÀµÊý¾Ý¿â²úÆ·×ÔÉíÌØÐԵĵط½£¬ODBC ¶¼ÒýÈëÒ»¸ö¹«¹²½Ó¿ÚÒÔ½â¾öDZÔڵIJ»Ò»ÖÂÐÔ£¬´Ó¶øºÜºÃµØ½â¾öÁË»ùÓÚÊý¾Ý¿âϵͳӦÓóÌÐòµÄÏà¶Ô¶ÀÁ¢ÐÔ£¬ÕâÒ²ÊÇODBC Ò»¾­ÍƳö¾Í»ñµÃ¾Þ´ó³É¹¦µÄÖØÒªÔ­ÒòÖ®Ò»¡£

´Ó½á¹¹ÉÏ·Ö£¬ODBC ·ÖΪµ¥ÊøʽºÍ¶àÊøʽÁ½Àà¡£

µ¥ÊøʽÇý¶¯³ÌÐò

µ¥ÊøʽÇý¶¯³ÌÐò½éÓÚÓ¦ÓóÌÐòºÍÊý¾Ý¿âÖ®¼ä£¬ÏñÖнéÇý¶¯³ÌÐòÒ»ÑùÊý¾ÝÌṩһ¸öͳһµÄÊý¾Ý·ÃÎÊ·½Ê½¡£

µ±Óû§½øÐÐÊý¾Ý¿â²Ù×÷ʱ£¬Ó¦ÓóÌÐò´«µÝÒ»¸öODBC º¯Êýµ÷ÓøøODBC Çý¶¯³ÌÐò¹ÜÀíÆ÷£¬ÓÉODBC API Åжϸõ÷ÓÃÊÇÓÉËüÖ±½Ó´¦Àí²¢½«½á¹û·µ»Ø»¹ÊÇËͽ»Çý¶¯³ÌÐòÖ´Ðв¢½«½á¹û·µ»Ø¡£

ÓÉÉϿɼû£¬µ¥ÊøʽÇý¶¯³ÌÐò±¾ÉíÊÇÒ»¸öÊý¾Ý¿âÒýÇ棬ÓÉËüÖ±½Ó¿ÉÍê³É¶ÔÊý¾Ý¿âµÄ²Ù×÷£¬¾¡¹Ü¸ÃÊý¾Ý¿â¿ÉÄÜλÓÚÍøÂçµÄÈκεط½¡£

¶àÊøʽÇý¶¯³ÌÐò

¶àÊøʽÇý¶¯³ÌÐò¸ºÔðÔÚÊý¾Ý¿âÒýÇæºÍ¿Í»§Ó¦ÓóÌÐòÖ®¼ä´«ËÍÃüÁîºÍÊý¾Ý£¬Ëü±¾Éí²¢²»Ö´ÐÐÊý¾Ý´¦Àí²Ù×÷¶øÓÃÓÚÔ¶³Ì²Ù×÷µÄÍøÂçͨÐÅЭÒéµÄÒ»¸ö½çÃæ¡£

Ç°¶ËÓ¦ÓóÌÐòÌá³ö¶ÔÊý¾Ý¿â´¦ÀíµÄÇëÇ󣬸ÃÇëÇóת¸øODBC Çý¶¯³ÌÐò¹ÜÀíÆ÷£¬Çý¶¯³ÌÐò¹ÜÀíÆ÷ÒÀ¾ÝÇëÇóµÄÇé¿ö£¬¾ÍµØÍê³É»ò´«¸ø¶àÊøÇý¶¯³ÌÐò£¬¶àÊøʽÇý¶¯³ÌÐò½«ÇëÇó·­ÒëΪÌض¨³§¼ÒµÄÊý¾Ý¿âͨÐŽӿڣ¨ÈçOracle µÄSQLNet£©ËùÄÜÀí½âµÄÐÎʽ²¢½»ÓÚ½Ó¿ÚÈ¥´¦Àí£¬½Ó¿Ú°ÑÇëÇó¾­ÍøÂç´«Ë͸ø·þÎñÆ÷ÉϵÄÊý¾ÝÒýÇ棬·þÎñÆ÷´¦ÀíÍêºó°Ñ½á¹û·¢»Ø¸øÊý¾Ý¿âͨÐŽӿڣ¬Êý¾Ý¿â½Ó¿Ú½«½á¹û´«¸ø¶àÊøʽODBC Çý¶¯³ÌÐò£¬ÔÙÓÉÇý¶¯³ÌÐò½«½á¹û´«¸øÓ¦ÓóÌÐò¡£

ºÜ¶à³ÌÐòÔ±ÒѾ­Ìå»áµ½ÁËÔÚWindowsƽ̨ϵÄODBCµÄÒæ´¦£¬¶øÔÚLinux/UnixϽøÐÐÊý¾Ý¿â±à³ÌµÄʱºòÈ´²»µÃ²»¸ù¾Ý²»Í¬µÄÊý¾Ý¿âÀ´Ñ¡ÔñÌØÓеÄAPI½øÐбà³Ì£¬Ò»µ©Êý¾Ý¿â·¢ÉúÁ˸ı䣬ËùÓÐÓëÕâЩAPIÏà¹ØµÄ³ÌÐò¶¼±ØÐë½øÐÐÐ޸ġ£ÆäʵÔÚLinux/UnixÏÂÏÖÔÚÒ²ÓÐÁË×Ô¼ºµÄODBC£¬¿ÉÒÔʹÎÒÃǵÄÊý¾Ý¿â±à³Ì¾ÍÏñÔÚWindowsƽ̨ÏÂÒ»Ñù¼òµ¥¡£

ÏÂÃæÎÒÃÇ¿ªÊ¼½éÉÜLinux/UnixϵÄODBC£º

¶þ¡¢ Linux/UnixÏÂODBCµÄ°²×°£º

·½·¨Ò»£º

ÏÈÏÂÔØ×îеÄunixODBCÔ´Âë°ü£¨http://www.unixodbc.org/unixODBC-2.2.1.tar.gz£©·Åµ½/usr/localÏ£¬È»ºóÔËÐÐÏÂÊöÃüÁ

tar zxvf unixODBC-2.2.1.tar.gz

cd unixODBC-2.2.1

./configure --prefix=/usr/local/unixODBC-2.2.1 --includedir=/usr/include --libdir=/usr/lib -bindir=/usr/bin --sysconfdir=/etc

make

make install

°²×°³É¹¦ºó£¬unixODBCËùÐèµÄÍ·Îļþ¶¼±»°²×°µ½ÁË/usr/inlucdeÏ£¬±àÒëºÃµÄ¿âÎļþ°²×°µ½ÁË/usr/libÏ£¬ÓëunixODBCÏà¹ØµÄ¿ÉÖ´ÐÐÎļþ°²×°µ½ÁË/usr/binÏ£¬ÅäÖÃÎļþ·Åµ½ÁË/etcÏ¡£

·½·¨¶þ£º

ÏÂÔØrpm°ü½øÐа²×°£¬ÎÒÃÇÕâÀïÒÔRed Hat 7.3ΪÀý£º

unixODBC-2.2.0-5 RPM for i386£¨°²×°°ü¼°Ô´Âë°ü£©

£¨ftp: //speakeasy.rpmfind.net/linux/redhat/7.3/en/os/i386/RedHat/RPMS/unixODBC- 2.2.0-5.i386.rpm¡¢ftp: //ftp.rpmfind.net/linux/redhat/7.3/en/os/i386/SRPMS/unixODBC-2.2.0-5.src.rpm£©

unixODBC-devel-2.2.0-5 RPM for i386

£¨ftp://speakeasy.rpmfind.net/linux/redhat/7.3/en/os/i386/RedHat/RPMS/unixODBC-devel-2.2.0-5.i386.rpm£©

Ö±½Ó½«unixODBC-2.2.0-5.i386.rpmºÍunixODBC-devel-2.2.0-5.i386.rpm×°Èëϵͳ¾Í¿ÉÒÔÁË£¬ÃüÁîÈçÏ£º

rpm -ivh unixODBC-2.2.0-5.i386.rpm

rpm -ivh unixODBC-devel-2.2.0-5.i386.rpm

°²×°ºÃÒÔºó£¬ËùÐèµÄ¸÷¸ö²¿·ÖÓëÉÏÃæËùÁеÄλÖÃÏàͬ¡£

Èý¡¢ Linux/UnixÏÂODBCµÄÅäÖãº

ÔËÐÐODBCConfig³ÌÐò£¨ÔÚ/usr/binÏ£©£¬ÈçÏÂͼ£º



ͼһ£ºODBCConfigÖ÷´°¿Ú

ºÍWindowsϵÄODBCÉèÖô°¿ÚÊDz»ÊǺÜÏñ£¿ÎÒÏë´ó¼Ò¶¼ÄÜ¿´¶®°É¡£

µÚÒ»²½£º°²×°Êý¾Ý¿âµÄODBCÇý¶¯³ÌÐò

DriversÕâÒ»À¸ÖÐÓÃÀ´ÉèÖÃÊý¾Ý¿âµÄÇý¶¯³ÌÐò£¬µã»÷Add°´Å¥£¬»á³öÏÖÏÂͼ£º



ͼ¶þ£ºODBCConfig Driver Properties´°¿Ú

NameÒ»À¸ÌîÈëÊý¾Ý¿âÇý¶¯µÄÃû³Æ£¬DescriptionÊÇÊý¾Ý¿âÇý¶¯µÄÃèÊö£¬DriverÊÇÓÃÀ´Ñ¡ÔñÊý¾Ý¿âÇý¶¯³ÌÐòµÄ£¬SetupÊÇÓÃÀ´Ñ¡ÔñÊý¾Ý¿âÇý¶¯°²×°³ÌÐòµÄ£¬Èç¹ûÄãÊÇ°´ÕÕÉÏÊö°²×°·½·¨°²×°µÄ£¬ÕâЩ³ÌÐò¶¼·ÅÔÚ/usr/libÏ£¬ÏÂÃæÊÇÊý¾Ý¿âÇý¶¯³ÌÐòµÄÁÐ±í£º

Êý¾Ý¿â Êý¾Ý¿âÇý¶¯³ÌÐò Êý¾Ý¿âÇý¶¯°²×°³ÌÐò

TXT libodbctxt.so libodbctxtS.so

NNTP libnn.so libodbcnnS.so

MiniSQL libodbcmini.so libodbcminiS.so

PostgreSQL libodbcpsql.so libodbcpsqlS.so

MySQL £¨×¢ÊÍ£© libodbcmyS.so

Sybase/MS SQL £¨×¢ÊÍ£© libtdsS.so

Oracle £¨×¢ÊÍ£© liboraodbcS.so

×¢ÊÍ£º

MySQL¡¢Sybase/MS SQLºÍOracleµÄÊý¾Ý¿âÇý¶¯¿ÉÒÔÔÚÏÂÁÐÍøÖ·ÕÒµ½£º

MySQLhttp://www.unixodbc.org/myodbc.html

Sybase/MS SQLhttp://www.freetds.org

Oracle http://www.easysoft.org

MySQLµÄÇý¶¯³ÌÐòMyODBC-2.50.39-4 RPM for i386ÒÔ¼°Ô´Âë°ü£º

ftp://speakeasy.rpmfind.net/linux/redhat/7.3/en/os/i386/RedHat/RPMS/MyODBC-2.50.39-4.i386.rpm

ftp://ftp.redhat.com/pub/redhat/linux/7.3/en/os/i386/SRPMS/MyODBC-2.50.39-4.src.rpm

Ñ¡ÔñºÃÇý¶¯³ÌÐòÖ®ºó£¬µã»÷"¡Ì"±£´æÍ˳ö¡£

µÚ¶þ²½£ºÉèÖÃDSN

DSN·ÖΪUser DSN¡¢System DSNºÍFile DSNÈýÖÖ£¬ÎÒÃÇÒÔSystem DSNΪÀý¡£Ñ¡ÖÐSystem DSNÒ»À¸ÒԺ󣬵ã»÷Add¡­°´Å¥¾Í»á¼ûµ½ÏÂͼ£º



ͼÈý£º´´½¨DSNÑ¡ÔñÊý¾Ý¿âÇý¶¯

ÁбíÖлáÁгöÄãÒѾ­°²×°ºÃµÄÊý¾Ý¿âÇý¶¯³ÌÐò£¬ÎÒÕâÀïÖ»×°ÁËMySQLºÍPostgreSQL£¬È»ºóÑ¡ÔñÄãËùҪʹÓõÄÇý¶¯³ÌÐò£¬È»ºóµã»÷OK¾Í»á³öÏÖÏÂͼ£º



ͼËÄ£ºDSNµÄÉèÖÃ

ÎÒÕâÀïʹÓõÄÊÇMySQLµÄÊý¾Ý¿âÇý¶¯£¬²»Í¬µÄÊý¾Ý¿â£¬Õâ¸ö´°¿ÚµÄÄÚÈÝ»áÓÐËù²»Í¬¡£NameÊÇÊý¾ÝÔ´µÄÃû³Æ£¬DescriptionÊÇÃèÊö£¬ Server¿ÉÒÔÑ¡Ôñ·þÎñÆ÷£¬Èç¹û±¾»úÆô¶¯ÁËMySQL¾Í¿ÉÒÔÑ¡Ôñlocalhost£¬Èç¹ûPortºÍSocketÓÐÌØÊâÒªÇó£¬ÔÙ¸ù¾Ýʵ¼ÊÇé¿ö½øÐÐÐ޸ģ¬ DatabaseÊÇÓÃÀ´Ñ¡ÔñÊý¾Ý¿âµÄ£¬ÏÂÀ­²Ëµ¥²»Ò»¶¨°üº¬ËùÓеÄÊý¾Ý¿â£¬Äã¿ÉÒÔ°Ñ×Ô¼ºÒѾ­´´½¨ºÃµÄÊý¾Ý¿âÃû³ÆÌîдÔÚÕâÀï¡£¶¼ÅäÖúÃÖ®ºó£¬µã»÷"¡Ì"±£´æÍ˳ö¡£

ÕâÑùLinux/UnixϵÄODBCÊý¾ÝÔ´¾ÍÒѾ­ÉèÖúÃÁË£¬´ó¼Ò»¹¿ÉÒÔÔÚODBCConfig³ÌÐòµÄStatusÀ¸Öв鿴 ODBCµÄʹÓÃÇé¿ö£¬ÔÚAdvancedÀ¸ÖÐÉèÖÃÊÇ·ñ×öÈÕÖ¾»òÕßÆô¶¯Á¬½Ó³Ø£¬ÔÚAboutÀ¸ÖУ¬ÓÐÒ»¸öLinux/Unix ODBCµÄʾÒâͼ£¬ÔÚCredits°´Å¥ÖпÉÒÔ¿´µ½ËùÓпª·¢ÕßµÄÃû×ÖµÄÁÐ±í¡£ ODBCConfig³ÌÐòÖÐËùÓÐÓйØÊý¾Ý¿âÇý¶¯³ÌÐòµÄÐÅÏ¢±»·ÅÔÚodbcinst.ini£¨ÔÚ/etcÏ£©ÎļþÖУ¬ÓйØDSNµÄÐÅÏ¢±»·ÅÔÚ odbc.ini£¨ÔÚ/etcÏ£©ÎļþÖУ¬´ó¼ÒÓÐÐËȤµÄ»°£¬¿ÉÒÔ×Ô¼ºÈ¥¹Û²ìһϡ£

µÚÈý²½£ºÊ¹ÓÃDataManager³ÌÐòä¯ÀÀÊý¾Ý¿â

ÔËÐÐDataManager³ÌÐòÖ®ºó¾Í¿ÉÒԲ鿴Drivers¡¢System DSNºÍUser DSNÕ⼸ÏîÄÚÈÝ£¬£¬ÔÚä¯ÀÀÊý¾Ý¿âµÄʱºò£¬¿ÉÒÔÔÚÓÒÃæµÄSQLÀ¸ÖÐÊäÈëSQLÓï¾ä£¬È»ºóµã»÷ÈËÐΰ´Å¥¾Í¿ÉÒÔÔËÐÐSQLÓï¾ä£¬ÔËÐнá¹û»áÔÚResultsÒ»À¸ÖÐÏÔʾ³öÀ´£¬¾ßÌåÇé¿ö¿ÉÒÔ¼ûÏÂͼ£º



ͼÎ壺ʹÓÃDataManagerä¯ÀÀÊý¾Ý¿â

µÚËIJ½£ºÊ¹ÓÃisql³ÌÐò²é¿´Êý¾Ý¿â

unixODBC»¹ÌṩÁËÃüÁį̂ϲ鿴Êý¾Ý¿âµÄ³ÌÐò£¬Õâ¾ÍÊÇisql£¬Ó÷¨ÈçÏ£º

isql DSN [UID [PWD]] [options]

DSN Êý¾ÝÔ´Ãû³Æ

UID Óû§ID

PWD Óû§ÃÜÂë

Options£º

-b Åú´¦Àí£¬Ã»ÓÐÌáʾ·ûµÄģʽ

-dx ÉèÖÃÁÐÖ®¼äµÄ·Ö¸ô·ûΪx

-w ½«²éѯ½á¹ûÊä³öΪHTML¸ñʽ

-c µÚÒ»ÐÐÊä³öÁÐÃû

--version ¡¡ Êä³öisqlµÄ°æ±¾ºÅ

ËÄ¡¢ Linux/UnixÏÂODBCµÄ±à³Ì£º

1¡¢Ê¹ÓÃunixODBCÌṩµÄODBC API½øÐбà³Ì£º

ÔÚ½øÐбà³Ì֮ǰ£¬ÎÒÃÇÀ´¿´Ò»ÏÂODBC APIÖеij£ÓÃÊý¾ÝÀàÐÍÓëÎÒÃÇÔÚCÓïÑÔÖÐʹÓõÄÊý¾ÝÀàÐ͵ĶÔÓ¦¹Øϵ£º

ÀàÐͱêʶ·û ODBCÊý¾ÝÀàÐÍ CÊý¾ÝÀàÐÍ

SQL_C_CHAR SQLCHAR * unsigned char *

SQL_C_SSHORT SQLSMALLINT short int

SQL_C_USHORT SQLUSMALLINT unsigned short int

SQL_C_SLONG SQLINTEGER long int

SQL_C_FLOAT SQLREAL float

SQL_C_DOUBLE SQLDOUBLE, SQLFLOAT double

SQL_C_BINARY SQLCHAR * unsigned char *

SQL_C_TYPE_DATE SQL_DATE_STRUCT struct tagDATE_STRUCT {SQLSMALLINT year; SQLUSMALLINT month; SQLUSMALLINT day; } DATE_STRUCT;

SQL_C_TYPE_TIME SQL_TIME_STRUCT struct tagTIME_STRUCT {SQLUSMALLINT hour; SQLUSMALLINT minute; SQLUSMALLINT second; } TIME_STRUCT;

ÎÒÃÇÕâÀïʹÓõÄÊý¾Ý¿âÃû³ÆΪtest£¨DSN£©£¬Õâ¸öDSNʹÓõÄÓû§ÃûÊÇroot£¬ÃÜÂëΪ¿Õ£¬±íµÄÃû³ÆÊÇweb£¬×Ö¶ÎÇé¿öÈçÏ£º

>¡¡×Ö¶ÎÃû Êý¾ÝÀàÐÍ

id integer

name char(40)

size integer

µÚÒ»£ºÉ趨ODBC»·¾³¾ä±ú²¢ÉèÖòÎÊý

Ê×ÏÈÎÒÃÇÐèÒªÉùÃ÷Ò»¸öODBC»·¾³¾ä±ú£¨SQLHENV£©£¬Ëü¿ÉÒÔÓÃÀ´»ñµÃÓйصÄODBC»·¾³ÐÅÏ¢£¬ÎÒÃÇÐèÒªµ÷ÓÃSQLAllocHandle ( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &V_OD_Env )À´»ñµÃÕâ¸ö¾ä±ú£¬V_OD_Env¾ÍÊÇÒª·ÖÅäµÄSQLHENVÀàÐ͵Ļ·¾³¾ä±ú¡£

·ÖÅäºÃ¾ä±úÖ®ºó£¬Äã¸øËüÐèÒªÉ趨ËùʹÓõÄODBC°æ±¾£¬Äã¿ÉÒÔµ÷ÓÃSQLSetEnvAttr ( V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0 )£¬SQL_ATTR_ODBC_VERSIONÊÇ´æ·ÅÄ㶨ÒåµÄODBC°æ±¾ºÅµÄ±äÁ¿£¬SQL_OV_ODBC3Ôò˵Ã÷ÄãµÄ³ÌÐòʹÓõÄÊÇODBC 3.0¡£

µÚ¶þ£ºÉ趨Á¬½Ó¾ä±ú²¢ÉèÖó¬Ê±²ÎÊý

ÎÒÃÇÐèÒªÉùÃ÷Ò»¸öÁ¬½Ó¾ä±ú£¨SQLHDBC£©£¬ÓÃÀ´´æ·ÅÊý¾Ý¿âÁ¬½ÓÐÅÏ¢µÄ£¬µ÷ÓÃSQLAllocHandle ( SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc )»ñµÃÁ¬½Ó¾ä±ú£¬V_OD_hdbc¾ÍÊÇÒª·ÖÅäµÄSQLHDBCÀàÐ͵ÄÁ¬½Ó¾ä±ú¡£

·ÖÅäºÃÖ®ºó£¬ÎÒÃÇ¿ÉÒÔµ÷ÓÃSQLSetConnectAttr ( V_OD_hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0 )À´É趨Á¬½Ó³¬Ê±²ÎÊý¡£

µÚÈý£ºÁ¬½ÓÊý¾Ý¿â

µ÷ÓÃSQLConnect ( V_OD_hdbc, (SQLCHAR*) "Test", SQL_NTS, (SQLCHAR*) "root", SQL_NTS, (SQLCHAR*) "", SQL_NTS )Á¬½ÓÎÒÇ°ÃæÌáµ½µÄÊý¾Ý¿â£¬ÐèÒªÉ趨Èý¸ö²ÎÊý£¬¾ÍÊÇÊý¾Ý¿âÃû³Æ¡¢Óû§ÃûºÍÃÜÂ루ÒòΪÎÒµÄÊý¾Ý¿âÃÜÂëΪ¿Õ£¬ËùÒÔÕâÀïµÄÃÜÂëҲΪ¿Õ£©£¬ºóÃæµÄSQL_NTSµÄλÖÃÓ¦¸ÃдÈëÕâЩ²ÎÊýµÄ³¤¶È£¬Èç¹ûдµÄÊÇSQL_NTS¾ÍÊÇÈÃSQLConnectÀ´¾ö¶¨²ÎÊýµÄ³¤¶È¡£

µÚËÄ£º·ÖÅäSQLÓï¾äµÄ¾ä±ú²¢½øÐвéѯ£º

ÐèÒªÉùÃ÷Ò»¸öSQLÓï¾äµÄ¾ä±ú£¨SQLHSTMT£©£¬ÓÃÀ´´æ·ÅSQLÓï¾äÐÅÏ¢µÄ£¬µ÷ÓÃSQLAllocHandle ( SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt )À´»ñµÃÕâ¸ö¾ä±ú£¬V_OD_hstmt¾ÍÊÇÎÒÃÇÒª·ÖÅäµÄSQLHSTMTÀàÐ͵ÄSQLÓï¾ä¾ä±ú¡£

ÎÒÃǵIJéѯÓï¾äÊÇ£º

SELECT name, id FROM web ORDER BY id

Ö´ÐÐÕâÌõ²éѯÓï¾äÖ®ºó£¬²éѯ½á¹û¿ÉÄÜÓкܶàÐУ¬µ«Ã¿ÐÐÖ»ÓÐÁ½ÁУ¬·Ö±ð¶ÔÓ¦nameºÍid£¬ËüÃǵÄÊý¾ÝÀàÐÍΪintegerºÍchar*£¬ÔÚODBCÖеÄÊý¾ÝÀàÐͱêʶ·ûΪSQL_C_ULONGºÍSQL_C_CHAR¡£ÎÒÃÇÐèÒªÏÈÉùÃ÷ÕâÑùµÄÁ½¸ö±äÁ¿À´´æÖü²éѯ½á¹û£º

SQLINTEGER V_OD_id;

char V_OD_buffer[200];

È»ºóÎÒÃÇÐèҪʹÓÃSQLBindColº¯Êý°Ñ²éѯ½á¹ûºÍÎÒÃǶ¨ÒåµÄ±äÁ¿½øÐа󶨣º

SQLBindCol(V_OD_hstmt,1,SQL_C_CHAR, &V_OD_buffer,150,&V_OD_err);

SQLBindCol(V_OD_hstmt,2,SQL_C_ULONG,&V_OD_id,150,&V_OD_err);

ÕâÀïµÄV_OD_errÊÇÓÃÀ´´æ·Å´íÎóÐÅÏ¢±àºÅµÄ±äÁ¿£¬ÀàÐÍÒ²ÊÇSQLINTEGER¡£

½ÓÏÂÀ´£¬ÎÒÃǵ÷ÓÃSQLExecDirectÀ´½øÐвéѯ£º

SQLExecDirect ( V_OD_hstmt, "SELECT dtname,iduser FROM web order by iduser", SQL_NTS );

ÎÒÃÇ¿ÉÒÔÓÃSQLNumResultCols ( V_OD_hstmt, &V_OD_colanz )À´»ñµÃ½á¹ûµÄÁÐÊý£¬Ò²¿ÉÒÔÓÃSQLRowCount( V_OD_hstmt, &V_OD_rowanz )À´»ñµÃ½á¹ûµÄÌõÊý£¬V_OD_colanzºÍV_OD_rowanz·Ö±ð´æ´¢ÏàÓ¦µÄ½á¹û£¬ÀàÐÍ·Ö±ðΪSQLSMALLINTºÍSQLINTEGER¡£

ÔÚ¶ÁÈ¡½á¹û֮ǰ£¬ÎÒÃÇÐèÒªµ÷ÓÃSQLFetch ( V_OD_hstmt )Óï¾ä£¬Õâ¸öÓï¾ä¿ÉÒÔÓÃÀ´»ñµÃµÚÒ»Ìõ½á¹ûÒ²¿ÉÒÔÓÃÀ´¶¼ÏÂÒ»Ìõ£¬ÓеãÏñnextµÄ¸Ð¾õ¡£È»ºóÎÒÃǾͿÉÒÔÔÚV_OD_idºÍV_OD_bufferÀïÃæ»ñµÃÿÌõ¼Ç¼µÄ½á¹ûÁË¡£

µÚÎ壺¹ØÓڹرÕÁ¬½ÓºÍÊͷžä±ú

¹Ø±ÕÊý¾Ý¿âµÄÁ¬½Ó£¬µ÷ÓÃSQLDisconnect ( V_OD_hdbc )¾Í¿ÉÒÔÁË£¬µ«ÔڹرÕÊý¾Ý¿â֮ǰÐèÒªÏÈÊÍ·ÅSQLÓï¾äµÄ¾ä±ú£¬¶øÇÒÔڹرÕÊý¾Ý¿âÖ®ºóÓ¦¸ÃÊÍ·ÅÁ¬½Ó¾ä±úºÍODBC»·¾³¾ä±ú£¬Óï¾äÈçÏ£¨°´Õý³£µÄ˳Ðò£©£º

SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);

SQLDisconnect(V_OD_hdbc);

SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);

SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);

µÚÁù£º¹ØÓÚÉÏÊöÇé¿öÖеĴíÎóÐÅÏ¢´¦Àí

ÎÒÃÇÐèÒª¶¨ÒåÁ½¸ö±äÁ¿£º

long V_OD_erg;

SQLINTEGER V_OD_err;

SQLAllocHandle¡¢SQLSetEnvAttr¡¢SQLSetConnectAttr¡¢SQLConnect¡¢ SQLExecDirect¡¢SQLNumResultColsºÍSQLRowCountµÄµ÷Óýá¹û¶¼¿ÉÒÔÓÃV_OD_ergÀ´´æ´¢£¬V_OD_err¿ÉÒÔ»ñµÃSQLBindColÖеĴíÎóÐÅÏ¢¡£

µÚÆߣº»ñµÃ±¾»úµÄDSNÐÅÏ¢

ÎÒÃÇ¿ÉÒÔÔÚÉùÃ÷SQLHENV¾ä±úÖ®ºó£¬Ê¹ÓÃSQLDataSourcesº¯ÊýÀ´»ñµÃ±¾»úµÄDSNÐÅÏ¢¡£³ÌÐòÈçÏ£º

void OD_ListDSN(void)

{

char¡¡ l_dsn[100],l_desc[100];

short int¡¡l_len1,l_len2,l_next;

l_next=SQL_FETCH_FIRST;

while( SQLDataSources(V_OD_Env,l_next,l_dsn, sizeof(l_dsn),

&l_len1, l_desc, sizeof(l_desc), &l_len2) == SQL_SUCCESS)

{

printf("Server=(%s) Beschreibung=(%s)\n",l_dsn,l_desc);

l_next=SQL_FETCH_NEXT;

}

}

l_next±äÁ¿ÊÇÓÃÀ´Ö¸¶¨ÎÒÃÇËùÒª»ñµÃµÄDSNµÄÀà±ð£º

SQL_FETCH_FIRST É趨SQLDataSources()º¯ÊýÕÒµ½µÚÒ»¸ö¿ÉÓõÄÊý¾ÝÔ´£¨¿ÉÒÔÊÇUser DSN£¬Ò²¿ÉÒÔÊÇSysterm DSN£©

SQL_FETCH_FIRST_USER É趨SQLDataSources()º¯ÊýÕÒµ½µÚÒ»¸öUser DSN

SQL_FETCH_FIRST_SYSTEM É趨SQLDataSources()º¯ÊýÕÒµ½µÚÒ»¸öSystem DSN

SQL_FETCH_NEXT ÕÒµ½ÏÂÒ»¸öÊý¾ÝÔ´£¬ÖÁÓÚÊý¾ÝÔ´ÀàÐÍÔòÒª¸ù¾ÝÇ°ÃæµÄ¶¨Òå

µ½ÕâÀÎÒÃÇÔÚUnixµÄCÓïÑÔÏÂÃæ½øÐÐODBC±à³ÌÒѾ­½²Í꣬ÉÏÊöODBC APIÐèÒªÒýÓÃÒÔϼ¸¸öÍ·Îļþ£¨ÕâЩÎļþÒѾ­°²×°µ½/usr/includeÏÂÁË£©£º

#include

#include

#include

ÁíÍâÈç¹û´ó¼ÒʹÓÃGTK½øÐбà³Ì£¬ÓÉÓÚµ½Ä¿Ç°ÎªÖ¹GTK»¹Ã»ÓмÓÈëרÃÅ´¦ÀíÊý¾Ý¿âµÄ²¿¼þ£¬ËùÒÔ´ó¼Ò¿ÉÒÔÔÚGTKÖе÷ÓÃÉÏÊöµÄODBC API¼´¿É¡£

ÕâÀ︽ÉÏÀý³Ì¹©´ó¼Ò²Î¿¼Ñ§Ï°£º

/* odbc.c

testing unixODBC

*/

#include

#include

#include

#include

#include

SQLHENV V_OD_Env; // Handle ODBC environment

long V_OD_erg; // result of functions

SQLHDBC V_OD_hdbc; // Handle connection

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(int argc,char *argv[])

{

// 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*) "Test", SQL_NTS,

(SQLCHAR*) "root", SQL_NTS,

(SQLCHAR*) "", 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,

¡¡V_OD_stat, &V_OD_err,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, V_OD_stat,&V_OD_err,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);

}

SQLBindCol(V_OD_hstmt,1,SQL_C_CHAR, &V_OD_buffer,150,&V_OD_err);

SQLBindCol(V_OD_hstmt,2,SQL_C_ULONG,&V_OD_id,150,&V_OD_err);

V_OD_erg=SQLExecDirect(V_OD_hstmt,"SELECT dtname,iduser FROM web order by iduser",SQL_NTS);¡¡

if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))

{

¡¡ printf("Error in Select %d\n",V_OD_erg);

¡¡ SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);

¡¡ printf("%s (%d)\n",V_OD_msg,V_OD_err);

¡¡ SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);

¡¡ SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);

¡¡ SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);

¡¡ exit(0);

}

V_OD_erg=SQLNumResultCols(V_OD_hstmt,&V_OD_colanz);

if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))

{

SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);

SQLDisconnect(V_OD_hdbc);

SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);

SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);

exit(0);

}

printf("Number of Columns %d\n",V_OD_colanz);

V_OD_erg=SQLRowCount(V_OD_hstmt,&V_OD_rowanz);

if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))

{

¡¡printf("Number of RowCount %d\n",V_OD_erg);

¡¡SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);

¡¡SQLDisconnect(V_OD_hdbc);

¡¡SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);

¡¡SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);

¡¡exit(0);

}

printf("Number of Rows %d\n",V_OD_rowanz);

V_OD_erg=SQLFetch(V_OD_hstmt);¡¡

while(V_OD_erg != SQL_NO_DATA)

{

printf("Result: %d %s\n",V_OD_id,V_OD_buffer);

V_OD_erg=SQLFetch(V_OD_hstmt);¡¡

}¡¡;

SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);

SQLDisconnect(V_OD_hdbc);

SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);

SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);

return(0);

}

2£®QTϽøÐÐODBC±à³Ì

QT 3.0ÌṩÁËData Table¡¢Data BrowserºÍData ViewÈý¸öÓëÊý¾Ý¿âÏà¹ØµÄ¿Ø¼þ¡£Äã¿ÉÒÔÔÚQTµÄProjectÉèÖÃÄãÒªÁ¬½ÓµÄÊý¾Ý¿â£¬DriverÒ»À¸ÖÐÑ¡ÔñQODBC3¼´¿É£¬ÆäËüÑ¡ÏîÄãÒ»¿´¾ÍÃ÷°×ÁË¡£ÉÏÊöµÄÈý¸öÊý¾Ý¿â¿Ø¼þµÄʹÓ÷½·¨¿ÉÒԲμûQTÖÐÏàÓ¦Îĵµ£¬Ò²ºÜºÃʹÓõġ£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 5 ÌõÆÀÂÛ

  1. 221.226.124.* ÓÚ 2007-01-15 13:34:27·¢±í:

    ÔÚsolaris²Ù×÷ϵͳÏ£¬Ö´ÐÐunixODBC-2.2.12µÄ./configureºóÉú³ÉµÄȱʡmakefileÊÇ·ñÊÇ32λ±àÒëµÄ£¬¶øÇÒunixODBCÓÖÒÀÀµÓÚQT£¬ÎÒÃÇϵͳʹÓõÄQT.libÊÇ64λµÄ£¬¹ÊÁ¬½ÓunixODBCʱʧ°Ü£¬ÇëÎÊÈçºÎ½â¾ö£¿

  2. voff12 ÓÚ 2006-08-17 15:15:28·¢±í:

    ¸Ðл·ÖÏí

  3. voff12 ÓÚ 2006-08-17 15:15:10·¢±í:

    ¸Ðл·ÖÏí

  4. voff12 ÓÚ 2006-08-17 15:15:04·¢±í:

    ¸Ðл·ÖÏí

  5. pkncoin ÓÚ 2006-07-31 00:06:54·¢±í:

    ·¹ýѧϰ