File: my_param.c

package info (click to toggle)
myodbc 3.51.11-6.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,056 kB
  • ctags: 1,933
  • sloc: ansic: 31,656; sh: 10,955; cpp: 2,439; makefile: 1,120
file content (366 lines) | stat: -rw-r--r-- 9,972 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Copyright (C) 1995-2003 MySQL AB

   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 program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/***************************************************************************
			  my_param.c  -  description
			     ---------------------
    begin		 : Wed Sep 8 2001
    copyright		 : (C) MySQL AB 1995-2002, www.mysql.com
    author		 : venu ( venu@mysql.com )
 ***************************************************************************/

/***************************************************************************
 *									   *
 *  This is a basic sample to demonstrate how to insert or delete or	   *
 *  update data in the table using parameters				   *
 *									   *
 ***************************************************************************/

#include "my_utility.h" /* MyODBC 3.51 sample utility header */

/********************************************************
 * initialize tables					*
 *********************************************************/
void my_init_table(SQLHDBC hdbc, SQLHSTMT hstmt)
{
  SQLRETURN   rc;

  printf("\nmy_init_table:\n");

  /* drop table 'my_demo_param' if it already exists */
  printf(" creating table 'my_demo_param'\n");

  rc = SQLExecDirect(hstmt,(SQLCHAR*) "DROP TABLE if exists my_demo_param",
		     SQL_NTS);
  mystmt(hstmt,rc);

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  mycon(hdbc,rc);

  /* create the table 'my_demo_param' */
  rc = SQLExecDirect(hstmt,(SQLCHAR*) "\
CREATE TABLE my_demo_param(\
id   int,\
auto int primary key auto_increment,\
name varchar(20),\
timestamp timestamp(14))",
		     SQL_NTS);
  mystmt(hstmt,rc);

  /* commit the transaction*/
  rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  mycon(hdbc,rc);
}

/********************************************************
 * prints the statement resultset			*
 *********************************************************/
int my_print_resultset(SQLHSTMT hstmt)
{
  SQLRETURN   rc;
  SQLUINTEGER nRowCount=0;
  SQLULEN     pcColDef;
  SQLCHAR     szColName[MAX_NAME_LEN];
  SQLCHAR     szData[MAX_COLUMNS][MAX_ROW_DATA_LEN]={0};
  SQLSMALLINT nIndex,ncol,pfSqlType, pcbScale, pfNullable;

  /* get total number of columns from the resultset */
  rc = SQLNumResultCols(hstmt,&ncol);
  mystmt(hstmt,rc);

  /* 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);

  return(nRowCount);
}

/********************************************************
 * insert data using parameters				*
 *********************************************************/
void my_param_insert(SQLHDBC hdbc, SQLHSTMT hstmt)
{
  SQLRETURN   rc;
  SQLINTEGER  id;
  char	   name[50];

  printf("\nmy_param_insert:\n");

  /* prepare the insert statement with parameters */
  rc= SQLPrepare(hstmt,
		 (SQLCHAR*) "INSERT INTO my_demo_param(id,name) VALUES(?,?)",
		 SQL_NTS);
  mystmt(hstmt,rc);

  /* now supply data to parameter 1 and 2 */
  rc= SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
		       SQL_C_LONG, SQL_INTEGER, 0,0,
		       &id, 0, NULL);
  mystmt(hstmt,rc);

  rc= SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT,
		       SQL_C_CHAR, SQL_CHAR, 0,0,
		       name, sizeof(name), NULL);
  mystmt(hstmt,rc);

  /* now insert 10 rows of data */
  for (id= 0; id < 10; id++)
  {
    sprintf(name,"MySQL%d",id);
    rc= SQLExecute(hstmt);
    mystmt(hstmt,rc);
  }

  /* Free statement param resorces */
  rc= SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
  mystmt(hstmt,rc);

  /* Free statement cursor resorces */
  rc = SQLFreeStmt(hstmt, SQL_CLOSE);
  mystmt(hstmt,rc);

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  mycon(hdbc,rc);

  /* Now fetch and verify the data */
  rc = SQLExecDirect(hstmt, (SQLCHAR*) "SELECT * FROM my_demo_param", SQL_NTS);
  mystmt(hstmt,rc);

  assert(10 == my_print_resultset(hstmt));
}

/********************************************************
 * update data using parameters				*
 *********************************************************/
void my_param_update(SQLHDBC hdbc, SQLHSTMT hstmt)
{
  SQLRETURN   rc;
  SQLINTEGER  id=9;
  SQLLEN      nRowCount;
  SQLCHAR     name[]="update";

  printf("\nmy_param_update:\n");

  /* prepare the insert statement with parameters */
  rc= SQLPrepare(hstmt,
		 (SQLCHAR*) "UPDATE my_demo_param set name = ? WHERE id = ?",
		 SQL_NTS);
  mystmt(hstmt,rc);

  /* now supply data to parameter 1 and 2 */
  rc= SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
		       SQL_C_CHAR, SQL_CHAR, 0,0,
		       name, sizeof(name), NULL);
  mystmt(hstmt,rc);

  rc= SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT,
		       SQL_C_LONG, SQL_INTEGER, 0,0,
		       &id, 0, NULL);
  mystmt(hstmt,rc);

  /* now execute the update statement */
  rc= SQLExecute(hstmt);
  mystmt(hstmt,rc);

  /* check the rows affected by the update statement */
  rc= SQLRowCount(hstmt, &nRowCount);
  mystmt(hstmt,rc);
  printf("\n total rows updated:%ld\n",nRowCount);
  assert(nRowCount == 1);

  /* Free statement param resorces */
  rc= SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
  mystmt(hstmt,rc);

  /* Free statement cursor resorces */
  rc= SQLFreeStmt(hstmt, SQL_CLOSE);
  mystmt(hstmt,rc);

  /* commit the transaction */
  rc= SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  mycon(hdbc,rc);

  /* Now fetch and verify the data */
  rc= SQLExecDirect(hstmt, (SQLCHAR*) "SELECT * FROM my_demo_param",SQL_NTS);
  mystmt(hstmt,rc);

  assert(10 == my_print_resultset(hstmt));
}


/********************************************************
 * delete data using parameters				*
 *********************************************************/
void my_param_delete(SQLHDBC hdbc, SQLHSTMT hstmt)
{
  SQLRETURN   rc;
  SQLINTEGER  id;
  SQLLEN      nRowCount;

  printf("\nmy_param_delete:\n");

  /* supply data to parameter 1 */
  rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
			SQL_C_LONG, SQL_INTEGER, 0,0,
			&id, 0, NULL);
  mystmt(hstmt,rc);

  /* execute the DELETE STATEMENT to delete 5th row  */
  id = 5;
  rc = SQLExecDirect(hstmt,
		     (SQLCHAR*) "DELETE FROM my_demo_param WHERE id = ?",
		     SQL_NTS);
  mystmt(hstmt,rc);

  /* check the rows affected by the update statement */
  rc = SQLRowCount(hstmt, &nRowCount);
  mystmt(hstmt,rc);
  printf(" total rows deleted:%ld\n",nRowCount);
  assert( nRowCount == 1);

  /* execute the DELETE STATEMENT to delete 8th row  */
  id = 8;
  rc = SQLExecDirect(hstmt,
		     (SQLCHAR*) "DELETE FROM my_demo_param WHERE id = ?",
		     SQL_NTS);
  mystmt(hstmt,rc);

  /* check the rows affected by the update statement */
  rc = SQLRowCount(hstmt, &nRowCount);
  mystmt(hstmt,rc);
  printf(" total rows deleted:%ld\n",nRowCount);
  assert( nRowCount == 1);

  /* Free statement param resorces */
  rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
  mystmt(hstmt,rc);

  /* Free statement cursor resorces */
  rc = SQLFreeStmt(hstmt, SQL_CLOSE);
  mystmt(hstmt,rc);

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  mycon(hdbc,rc);

  /* Now fetch and verify the data */
  rc = SQLExecDirect(hstmt, (SQLCHAR*) "SELECT * FROM my_demo_param",
		     SQL_NTS);
  mystmt(hstmt,rc);

  assert(8 == my_print_resultset(hstmt));
}

/********************************************************
 * 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_param [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);

  /*
   * initialize table
   */
  my_init_table(hdbc, hstmt);

  /*
   * insert data using parameters
   */
  my_param_insert(hdbc, hstmt);

  /*
   * parameter update
   */
  my_param_update(hdbc, hstmt);

  /*
   * parameter delete
   */
  my_param_delete(hdbc, hstmt);

  /*
   * disconnect from the server, by freeing all resources
   */
  mydisconnect(&henv,&hdbc,&hstmt);

  return(0);
}