1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/***************************************************************************
my_result.c - description
---------------------
begin : Wed Sep 8 2001
copyright : (C) MySQL AB 1995-2002, www.mysql.com
author : venu ( venu@mysql.com )
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/***************************************************************************
* *
* This is a basic sample to demonstrate how to get the resultset *
* using MySQL ODBC 3.51 driver *
* *
***************************************************************************/
#include "my_utility.h" /* MyODBC 3.51 sample utility header */
/********************************************************
* result set demo *
*********************************************************/
void my_resultset(SQLHDBC hdbc, SQLHSTMT hstmt)
{
SQLRETURN rc;
SQLUINTEGER nRowCount=0, pcColDef;
SQLCHAR szColName[MAX_NAME_LEN];
SQLCHAR szData[MAX_COLUMNS][MAX_ROW_DATA_LEN]={0};
SQLSMALLINT nIndex,ncol,pfSqlType, pcbScale, pfNullable;
printf("\nmy_resultset:\n");
/* drop table 'myodbc3_demo_result' if it already exists */
rc = SQLExecDirect(hstmt,"DROP TABLE if exists myodbc3_demo_result",SQL_NTS);
mystmt(hstmt,rc);
/* commit the transaction */
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
mycon(hdbc,rc);
/* create the table 'myodbc3_demo_result' */
rc = SQLExecDirect(hstmt,"CREATE TABLE myodbc3_demo_result(\
id int primary key auto_increment,\
name varchar(20))",SQL_NTS);
mystmt(hstmt,rc);
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
mycon(hdbc,rc);
/* insert 2 rows of data */
rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_result values(\
1,'MySQL')",SQL_NTS);
mystmt(hstmt,rc);
rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_result values(\
2,'MyODBC')",SQL_NTS);
mystmt(hstmt,rc);
/* commit the transaction */
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
mycon(hdbc,rc);
/* update second row */
rc = SQLExecDirect(hstmt,"UPDATE myodbc3_demo_result set name=\
'MyODBC 3.51' where id=2",SQL_NTS);
mystmt(hstmt,rc);
/* commit the transaction */
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
mycon(hdbc,rc);
/* now fetch back..*/
rc = SQLExecDirect(hstmt,"SELECT * from myodbc3_demo_result",SQL_NTS);
mystmt(hstmt,rc);
/* get total number of columns from the resultset */
rc = SQLNumResultCols(hstmt,&ncol);
mystmt(hstmt,rc);
printf(" total columns in resultset:%d\n\n",ncol);
/* print the column names and do the row bind */
for(nIndex = 1; nIndex <= ncol; nIndex++)
{
rc = SQLDescribeCol(hstmt,nIndex,szColName, MAX_NAME_LEN+1, NULL,
&pfSqlType,&pcColDef,&pcbScale,&pfNullable);
mystmt(hstmt,rc);
printf(" %s\t",szColName);
rc = SQLBindCol(hstmt,nIndex, SQL_C_CHAR, szData[nIndex-1],
MAX_ROW_DATA_LEN+1,NULL);
mystmt(hstmt,rc);
}
printf("\n--------------------\n");
/* now fetch row by row */
rc = SQLFetch(hstmt);
while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
{
nRowCount++;
for(nIndex=0; nIndex< ncol; nIndex++)
printf(" %s\t",szData[nIndex]);
printf("\n");
rc = SQLFetch(hstmt);
}
SQLFreeStmt(hstmt,SQL_UNBIND);
printf("\n total rows fetched:%d\n",nRowCount);
/* free the statement row bind resources */
rc = SQLFreeStmt(hstmt, SQL_UNBIND);
mystmt(hstmt,rc);
/* free the statement cursor */
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
mystmt(hstmt,rc);
printf(" success!!\n");
}
/********************************************************
* main routine *
*********************************************************/
int main(int argc, char *argv[])
{
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLINTEGER narg;
/*
* show the usage string when the user asks for this
*/
printf("***********************************************\n");
printf("usage: my_result [DSN] [UID] [PWD] \n");
printf("***********************************************\n");
/*
* if connection string supplied through arguments, overrite
* the default one..
*/
for(narg = 1; narg < argc; narg++)
{
if ( narg == 1 )
mydsn = argv[1];
else if ( narg == 2 )
myuid = argv[2];
else if ( narg == 3 )
mypwd = argv[3];
}
/*
* connect to MySQL server
*/
myconnect(&henv,&hdbc,&hstmt);
/*
* simple resultset demo
*/
my_resultset(hdbc, hstmt);
/*
* disconnect from the server, by freeing all resources
*/
mydisconnect(&henv,&hdbc,&hstmt);
return(0);
}
|