【原创】请教:在C语言与数据库连接中遇到难题了!
bquzaha
|
1#
bquzaha 发表于 2005-04-26 15:13
【原创】请教:在C语言与数据库连接中遇到难题了!
小女子向高人请教:
源程序如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sql.h" #include "sqlext.h" void check_return (RETCODE rc, HENV henv, HDBC hdbc, HSTMT hstmt ) { UCHAR state_str [SQL_MAX_MESSAGE_LENGTH]; SDWORD native_error; UCHAR error_msg [SQL_MAX_MESSAGE_LENGTH]; SWORD error_msg_avail = SQL_MAX_MESSAGE_LENGTH - 1; SWORD error_msg_len; RETCODE local_rc; if (rc != SQL_ERROR && rc != SQL_SUCCESS_WITH_INFO ) { return; } local_rc = SQL_Error (henv,hdbc,hstmt,state_str,&native_error,error_msg,error_msg_avail,&error_msg_len); if (local_rc != SQL_SUCCESS && rc !=SQL_SUCCESS_WITH_INFO) { fprintf(stderr,"Uninterpretable error; exiting \n"); exit (EXIT_FAILURE); } if (rc == SQL_SUCCESS_WITH_INFO) { fprintf(stderr,"Fatal Error %s: %s\n", state_str,error_msg); return; } fprintf(stderr,"Fatal Error %s: %s\n",state_str,error_msg); exit (EXIT_FAILURE); } /*----------------------------------------------------------------------*/ int main (void) { HENV henv = SQL_NULL_HENV; HDBC hdbc = SQL_NULL_HDBC; HSTMT hstmt = SQL_NULL_HSTMT; RETCODE rc; char buf[257]; short buflen; printf ("Initialize the environment structure.\n"); SQLAllocEnv (&henv); printf ("Initialize the connection structure.\n"); SQLAllocConnect (henv,&hdbc); printf("Load the ODBC driver.\n"); rc = SQLDriverConnect (hdbc,0,"DSN=baseball;UID=myloginWD=mypassword",SQL_NTS,(UCHAR*) buf,sizeof (buf),&buflen,SQL_DRIVER_COMPLETE); check_return (rc,henv,hdbc,hstmt); printf ("Initialize the statement structure.\n"); SQLAllocStmt (hdbc,&hstmt); /* now do something*/ printf ("Creat table table \"foo\".\n"); rc = SQLExecdirect (hstmt,"CREATE TABLE foo (bar INTEGER)", SQL_NTS); check_return (rc, henv, hdbc, hstmt); printf ("Insert values into table \"foo\".\n"); rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (1)", SQL_NTS); check_return (rc, henv, hdbc, hstmt); rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (2)", SQL_NTS); check_return (rc, henv, hdbc, hstmt); rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (3)", SQL_NTS); check_return (rc, henv, hdbc, hstmt); printf ("Drop table \"foo\".\n"); rc = SQLExecDirect (hstmt, "DROP TABLE foo", SQL_NTS); check_return (rc,henv,hdbc,hstmt); /* We're done:free resources and exit*/ printf ("Free the statement handle.\n"); SQLFreeStmt (hstmt,SQL_DROP); printf ("Disconnect from the data source.\n"); SQLDisconnect (hdbc); printf ("Free the connection structure.\n"); SQLFreeConnect (hdbc); printf ("Free the environment structure.\n"); SQLFreeEnv (henv); printf ("Goodbye!\n"); exit (EXIT_SUCCESS); } 我的程序编译、连接的结果是: [root@localhost database_experiment]# gcc odbc.c /tmp/ccldHxyK.o(.text+0x5a): In function `check_return': : undefined reference to `SQLError' /tmp/ccldHxyK.o(.text+0x130): In function `main': : undefined reference to `SQLAllocEnv' /tmp/ccldHxyK.o(.text+0x152): In function `main': : undefined reference to `SQLAllocConnect' /tmp/ccldHxyK.o(.text+0x18b): In function `main': : undefined reference to `SQLDriverConnect' /tmp/ccldHxyK.o(.text+0x1c7): In function `main': : undefined reference to `SQLAllocStmt' /tmp/ccldHxyK.o(.text+0x1ec): In function `main': : undefined reference to `SQLExecdirect' /tmp/ccldHxyK.o(.text+0x22b): In function `main': : undefined reference to `SQLExecdirect' /tmp/ccldHxyK.o(.text+0x25a): In function `main': : undefined reference to `SQLExecdirect' /tmp/ccldHxyK.o(.text+0x289): In function `main': : undefined reference to `SQLExecdirect' /tmp/ccldHxyK.o(.text+0x2c8): In function `main': : undefined reference to `SQLExecDirect' /tmp/ccldHxyK.o(.text+0x302): In function `main': : undefined reference to `SQLFreeStmt' /tmp/ccldHxyK.o(.text+0x320): In function `main': : undefined reference to `SQLDisconnect' /tmp/ccldHxyK.o(.text+0x33e): In function `main': : undefined reference to `SQLFreeConnect' /tmp/ccldHxyK.o(.text+0x35c): In function `main': : undefined reference to `SQLFreeEnv' collect2: ld returned 1 exit status 我的头文件都装全了啊!不知为什么?恳请高人指点!!万分感谢! |