1780.html
来自「著名的linux英雄站点的文档打包」· HTML 代码 · 共 674 行 · 第 1/3 页
HTML
674 行
&l_len1, l_desc, sizeof(l_desc), &l_len2) == SQL_SUCCESS)<br>
{<br>
printf("Server=(%s) Beschreibung=(%s)",l_dsn,l_desc);<br>
l_next=SQL_FETCH_NEXT;<br>
}<br>
}<br>
<br>
l_next变量是用来指定我们所要获得的DSN的类别:<br>
SQL_FETCH_FIRST 设定SQLDataSources()函数找到第一个可用的数据源(可以是User DSN,也可以是Systerm DSN)<br>
SQL_FETCH_FIRST_USER 设定SQLDataSources()函数找到第一个User DSN<br>
SQL_FETCH_FIRST_SYSTEM 设定SQLDataSources()函数找到第一个System DSN<br>
SQL_FETCH_NEXT 找到下一个数据源,至于数据源类型则要根据前面的定义<br>
<br>
到这里,我们在Unix的C语言下面进行ODBC编程已经讲完,上述ODBC API需要引用以下几个头文件(这些文件已经安装到/usr/include下了):<br>
<br>
#include <sql.h><br>
#include <sqlext.h><br>
#include <sqltypes.h><br>
<br>
另外如果大家使用GTK进行编程,由于到目前为止GTK还没有加入专门处理数据库的部件,所以大家可以在GTK中调用上述的ODBC API即可。<br>
<br>
这里附上例程供大家参考学习:<br>
<br>
/* odbc.c<br>
testing unixODBC<br>
*/<br>
#include <stdlib.h><br>
#include <stdio.h><br>
#include <odbc/sql.h><br>
#include <odbc/sqlext.h><br>
#include <odbc/sqltypes.h><br>
<br>
SQLHENV V_OD_Env; // Handle ODBC environment<br>
long V_OD_erg; // result of functions<br>
SQLHDBC V_OD_hdbc; // Handle connection<br>
<br>
char V_OD_stat[10]; // Status SQL<br>
SQLINTEGER V_OD_err,V_OD_rowanz,V_OD_id;<br>
SQLSMALLINT V_OD_mlen,V_OD_colanz;<br>
char V_OD_msg[200],V_OD_buffer[200];<br>
<br>
<br>
int main(int argc,char *argv[])<br>
{<br>
// 1. allocate Environment handle and register version <br>
V_OD_erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env);<br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Error AllocHandle");<br>
exit(0);<br>
}<br>
V_OD_erg=SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); <br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Error SetEnv");<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
// 2. allocate connection handle, set timeout<br>
V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc); <br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Error AllocHDB %d",V_OD_erg);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
SQLSetConnectAttr(V_OD_hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);<br>
// 3. Connect to the datasource "web" <br>
V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "Test", SQL_NTS,<br>
(SQLCHAR*) "root", SQL_NTS,<br>
(SQLCHAR*) "", SQL_NTS);<br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Error SQLConnect %d",V_OD_erg);<br>
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, <br>
V_OD_stat, &V_OD_err,V_OD_msg,100,&V_OD_mlen);<br>
printf("%s (%d)",V_OD_msg,V_OD_err);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
printf("Connected !");<br>
V_OD_erg=SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);<br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Fehler im AllocStatement %d",V_OD_erg);<br>
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);<br>
printf("%s (%d)",V_OD_msg,V_OD_err);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
SQLBindCol(V_OD_hstmt,1,SQL_C_CHAR, &V_OD_buffer,150,&V_OD_err);<br>
SQLBindCol(V_OD_hstmt,2,SQL_C_ULONG,&V_OD_id,150,&V_OD_err);<br>
<br>
V_OD_erg=SQLExecDirect(V_OD_hstmt,"SELECT dtname,iduser FROM web order by iduser",SQL_NTS); <br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Error in Select %d",V_OD_erg);<br>
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);<br>
printf("%s (%d)",V_OD_msg,V_OD_err);<br>
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);<br>
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
V_OD_erg=SQLNumResultCols(V_OD_hstmt,&V_OD_colanz);<br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);<br>
SQLDisconnect(V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
printf("Number of Columns %d",V_OD_colanz);<br>
V_OD_erg=SQLRowCount(V_OD_hstmt,&V_OD_rowanz);<br>
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))<br>
{<br>
printf("Number of RowCount %d",V_OD_erg);<br>
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);<br>
SQLDisconnect(V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
exit(0);<br>
}<br>
printf("Number of Rows %d",V_OD_rowanz);<br>
V_OD_erg=SQLFetch(V_OD_hstmt); <br>
while(V_OD_erg != SQL_NO_DATA)<br>
{<br>
printf("Result: %d %s",V_OD_id,V_OD_buffer);<br>
V_OD_erg=SQLFetch(V_OD_hstmt); <br>
} ;<br>
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);<br>
SQLDisconnect(V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);<br>
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);<br>
return(0);<br>
}<br>
<br>
2.QT下进行ODBC编程<br>
QT 3.0提供了Data Table、Data Browser和Data View三个与数据库相关的控件。你可以在QT的Project设置你要连接的数据库,Driver一栏中选择QODBC3即可,其它选项你一看就明白了。上述的三个数据库控件的使用方法可以参见QT中相应文档,也很好使用的。<br>
<br>
参考资料:<br>
<br>
1. 微软的ODBC主页:http://www.microsoft.com/data/odbc/;<br>
2. UnixODBC的主页:http://www.unixodbc.org;<br>
3. FreeODBC的主页:http://www.jepstone.net/FreeODBC/;<br>
4. EasySoft的主页:http://www.easysoft.com;<br>
5. TrollTech的QT 3.0文档主页:http://doc.trolltech.com/3.0/。<br>
<br>
个人简介:<br>
<br>
齐亮,有幸于2001年参与Happy Linux 3.0的研发工作,对Linux情有独钟,现在从事Linux/Windows跨平台应用的开发,欢迎您通过电子邮件 cavendish@eyou.com跟他联系,希望能与更多的朋友交流关于Linux方面的知识。<br>
<br>
<br>
到页首<br>
您对这篇文章的看法如何?<br>
真棒!(5) 好材料 (4) 一般;尚可 (3) 需提高 (2) 太差! (1)<br>
<br>
建议?<br>
<br>
<br>
(c) Copyright IBM Corp. 2001, (c) Copyright IBM China 2001, All Right Reserved<br>
关于 IBM | 隐私条约 | 法律条款 | 联系 IBM<br>
</FONT><br>
</TD>
</TR>
<TR>
<TD colSpan=2><FONT
class=middlefont></FONT><BR>
<FONT
class=normalfont>全文结束</FONT> </TD>
</TR>
<TR>
<TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2
height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
<TD vAlign=top width="20%"
background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2>
<DIV align=center>
<table class=tableoutline cellspacing=1 cellpadding=4
width="100%" align=center border=0>
<tr class=firstalt>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
<font class=normalfont><b>所有分类</b></font></td>
</tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td> </tr> </table></td></tr> </table>
</DIV></TD></TR>
<TR vAlign=top>
<TD width="80%">
<DIV align=center><BR>
</DIV>
</TD></TR></TBODY></TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee
border=0><TBODY>
<TR>
<TD width="50%">
<P><FONT class=middlefont>版权所有 © 2004 <A
href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
违者必究. </FONT></P>
</TD>
<TD width="50%">
<DIV align=right><FONT class=middlefont>Powered by: <A
href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
</TD></TR></TBODY></TABLE>
<CENTER></CENTER></TD></TR>
</TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?