| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1"> |
| 4 | <TITLE>executing a query</TITLE> |
| 5 | <link rel="stylesheet" Href="odbc.css"> |
| 6 | </HEAD> |
| 7 | <BODY bgcolor="white"> |
| 8 | <table width="90%" cols="3" border="0"> |
| 9 | <TR> |
| 10 | <TD colspan="3" class="big">Executing a query</TD> |
| 11 | </TR> |
| 12 | <TR> |
| 13 | <TD colspan="3"><P>If you want to execute a query you will need to specify |
| 14 | a handle (<CODE>SQL_HANDLE_STMT</CODE>) for a SQL-statement. In order to get one |
| 15 | you have to allocate one with <CODE><A HREF="gloss.html#alloc">SQLAllocHandle</A></CODE>. |
| 16 | </P> |
| 17 | Then you have to think about the SQL statement you want to execute. As I mentioned in the introduction |
| 18 | I assume that we have a table <CODE><A HREF="intro.html#tab">tkeyuser</A></CODE> which contains the following |
| 19 | data:</P> |
| 20 | <TABLE COLS=3 cellspacing="1" border="0" bgcolor="#eeeeee" cellpadding=2 width="50%"> |
| 21 | <TR> |
| 22 | <TH class="head">iduser</TH> |
| 23 | <TH class="head">dtname</TH> |
| 24 | <TH class="head">dtmaxSize</TH> |
| 25 | </TR> |
| 26 | <TR> |
| 27 | <TD>1</TD> |
| 28 | <TD>Christa</TD> |
| 29 | <TD>10000</TD> |
| 30 | </TR> |
| 31 | <TR> |
| 32 | <TD>2</TD> |
| 33 | <TD>Nicole</TD> |
| 34 | <TD>9000</TD> |
| 35 | </TR> |
| 36 | </TABLE> |
| 37 | <P> |
| 38 | In this example, we want to execute a query which returns all the rows for the |
| 39 | fields <CODE>iduser</CODE> and <CODE>dtname</CODE> in this table ordered |
| 40 | by <code>iduser</CODE>. So the SQL statement would be:</P> |
| 41 | <CODE> |
| 42 | SELECT iduser,dtname FROM tkeydata ORDER BY iduser |
| 43 | </CODE> |
| 44 | <P> |
| 45 | If you execute this statement you would get two rows each with two <A HREF="gloss.html#col">columns</A> of data. |
| 46 | This data has to be stored somewhere so that your program can actually use it, so you need to define a variable |
| 47 | for each of the columns. So you need to bind a column to variable in your program. Binding a variable automatically |
| 48 | stores the data of the column in the variable when you retrieve a result <A HREF="gloss.html#row">row</A> from the |
| 49 | connection. It is important that your variables match the <A HREF="gloss.html#dtyp">type</A> of the column in the table |
| 50 | within the database.</P> |
| 51 | <P> |
| 52 | So we need to bin column #1 to a variable of type <CODE>SQLINTEGER</CODE> and the second column to |
| 53 | a variable of type <CODE>char</CODE>. This is done by <CODE><A HREF="gloss.html#bind">SQLBindCol</A></CODE>. |
| 54 | Therefore we add the variables:</P> |
| 55 | <CODE class="list"> |
| 56 | SQLHSTMT V_OD_hstmt; <FONT COLOR="blue">// Handle for a statement</FONT><BR> |
| 57 | SQLINTEGER V_OD_err,V_OD_id;<BR> |
| 58 | char V_OD_buffer[200]; |
| 59 | </CODE> |
| 60 | <P> |
| 61 | Now we can bind the variables:</P> |
| 62 | <CODE class="list"><PRE> |
| 63 | SQLBindCol(V_OD_hstmt,1,SQL_C_CHAR, &V_OD_buffer,200,&V_OD_err); |
| 64 | SQLBindCol(V_OD_hstmt,2,SQL_C_ULONG,&V_OD_id,sizeof(V_OD_id),&V_OD_err); |
| 65 | </PRE> |
| 66 | </CODE> |
| 67 | <P> |
| 68 | Yes you should check for the return code of the function call. I'm to lazy to |
| 69 | code it here once again :( |
| 70 | </P> |
| 71 | Now we can execute the query by calling <A HREF="gloss.html#exec"><CODE>SQLExecDirect</CODE></A>: |
| 72 | </P> |
| 73 | <CODE class="list"><PRE> |
| 74 | V_OD_erg=SQLExecDirect(V_OD_hstmt, |
| 75 | "SELECT dtname,iduser FROM tkeyuser order by iduser",SQL_NTS); |
| 76 | if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) |
| 77 | { |
| 78 | printf("Error Select %d\n",V_OD_erg); |
| 79 | SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat, &V_OD_err, |
| 80 | V_OD_msg,100,&V_OD_mlen); |
| 81 | printf("%s (%d)\n",V_OD_msg,V_OD_err); |
| 82 | SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc); |
| 83 | SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt); |
| 84 | SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); |
| 85 | exit(0); |
| 86 | } |
| 87 | </PRE> |
| 88 | </CODE> |
| 89 | </TD> |
| 90 | </TR> |
| 91 | </TABLE> |
| 92 | </BODY> |
| 93 | </HTML> |