~ubuntu-branches/debian/sid/unixodbc/sid

« back to all changes in this revision

Viewing changes to Drivers/MySQL/samples/my_param.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2004-10-15 03:07:52 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041015030752-dzw4vhxlgycz3woj
Tags: 2.2.4-11
Brown paper bag me: conflicts do not write themselves just because
you add a line to the changelog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          my_param.c  -  description
 
3
                             ---------------------
 
4
    begin                : Wed Sep 8 2001
 
5
    copyright            : (C) MySQL AB 1995-2002, www.mysql.com
 
6
    author               : venu ( venu@mysql.com )
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
/***************************************************************************
 
19
 *                                                                         *
 
20
 *  This is a basic sample to demonstrate how to insert or delete or       *
 
21
 *  update data in the table using parameters                              *
 
22
 *                                                                         *
 
23
 ***************************************************************************/
 
24
 
 
25
#include "my_utility.h" /* MyODBC 3.51 sample utility header */
 
26
 
 
27
/********************************************************
 
28
* initialize tables                                     *
 
29
*********************************************************/
 
30
void my_init_table(SQLHDBC hdbc, SQLHSTMT hstmt)
 
31
{
 
32
  SQLRETURN   rc;
 
33
 
 
34
  printf("\nmy_init_table:\n");
 
35
 
 
36
    /* drop table 'my_demo_param' if it already exists */
 
37
    printf(" creating table 'my_demo_param'\n");
 
38
 
 
39
    rc = SQLExecDirect(hstmt,"DROP TABLE if exists my_demo_param",SQL_NTS);
 
40
    mystmt(hstmt,rc);
 
41
 
 
42
    /* commit the transaction */
 
43
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
44
    mycon(hdbc,rc);
 
45
 
 
46
    /* create the table 'my_demo_param' */
 
47
    rc = SQLExecDirect(hstmt,"CREATE TABLE my_demo_param(\
 
48
                              id   int,\
 
49
                              auto int primary key auto_increment,\
 
50
                              name varchar(20),\
 
51
                              timestamp timestamp(14))",SQL_NTS);
 
52
    mystmt(hstmt,rc);
 
53
 
 
54
    /* commit the transaction*/
 
55
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
 
56
    mycon(hdbc,rc);    
 
57
}
 
58
 
 
59
/********************************************************
 
60
* prints the statement resultset                        *
 
61
*********************************************************/
 
62
int my_print_resultset(SQLHSTMT hstmt)
 
63
{
 
64
  SQLRETURN   rc;
 
65
  SQLUINTEGER nRowCount=0, pcColDef;
 
66
  SQLCHAR     szColName[MAX_NAME_LEN];
 
67
  SQLCHAR     szData[MAX_COLUMNS][MAX_ROW_DATA_LEN]={0};
 
68
  SQLSMALLINT nIndex,ncol,pfSqlType, pcbScale, pfNullable;
 
69
 
 
70
    /* get total number of columns from the resultset */
 
71
    rc = SQLNumResultCols(hstmt,&ncol);
 
72
    mystmt(hstmt,rc);
 
73
 
 
74
    /* print the column names  and do the row bind */
 
75
    for(nIndex = 1; nIndex <= ncol; nIndex++)
 
76
    {
 
77
      rc = SQLDescribeCol(hstmt,nIndex,szColName, MAX_NAME_LEN+1, NULL,
 
78
                          &pfSqlType,&pcColDef,&pcbScale,&pfNullable);
 
79
      mystmt(hstmt,rc);
 
80
 
 
81
      printf(" %s\t",szColName);
 
82
 
 
83
      rc = SQLBindCol(hstmt,nIndex, SQL_C_CHAR, szData[nIndex-1],
 
84
                      MAX_ROW_DATA_LEN+1,NULL);
 
85
      mystmt(hstmt,rc);
 
86
    }
 
87
 
 
88
    printf("\n -------------------------------------------\n");
 
89
 
 
90
    /* now fetch row by row */
 
91
    rc = SQLFetch(hstmt);
 
92
    while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
 
93
    {
 
94
       nRowCount++;
 
95
       for(nIndex=0; nIndex< ncol; nIndex++)
 
96
         printf(" %s\t",szData[nIndex]);
 
97
     
 
98
       printf("\n");
 
99
       rc = SQLFetch(hstmt);
 
100
     }
 
101
     SQLFreeStmt(hstmt,SQL_UNBIND);
 
102
 
 
103
     printf("\n total rows fetched:%d\n",nRowCount);
 
104
 
 
105
     /* free the statement row bind resources */
 
106
     rc = SQLFreeStmt(hstmt, SQL_UNBIND);
 
107
     mystmt(hstmt,rc);
 
108
 
 
109
     /* free the statement cursor */
 
110
     rc = SQLFreeStmt(hstmt, SQL_CLOSE);
 
111
     mystmt(hstmt,rc);   
 
112
     
 
113
     return(nRowCount);
 
114
}
 
115
 
 
116
/********************************************************
 
117
* insert data using parameters                          *
 
118
*********************************************************/
 
119
void my_param_insert(SQLHDBC hdbc, SQLHSTMT hstmt)
 
120
{
 
121
  SQLRETURN   rc;
 
122
  SQLINTEGER  id;
 
123
  SQLCHAR     name[50];
 
124
 
 
125
  printf("\nmy_param_insert:\n");
 
126
 
 
127
    /* prepare the insert statement with parameters */
 
128
    rc = SQLPrepare(hstmt,"INSERT INTO my_demo_param(id,name) VALUES(?,?)",SQL_NTS);
 
129
    mystmt(hstmt,rc);
 
130
 
 
131
    /* now supply data to parameter 1 and 2 */
 
132
    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, 
 
133
                          SQL_C_LONG, SQL_INTEGER, 0,0,
 
134
                          &id, 0, NULL);
 
135
    mystmt(hstmt,rc);
 
136
 
 
137
    rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, 
 
138
                          SQL_C_CHAR, SQL_CHAR, 0,0,
 
139
                          name, sizeof(name), NULL);
 
140
    mystmt(hstmt,rc);
 
141
 
 
142
    /* now insert 10 rows of data */
 
143
    for (id = 0; id < 10; id++)
 
144
    {
 
145
      sprintf(name,"MySQL%d",id);
 
146
 
 
147
      rc = SQLExecute(hstmt);
 
148
      mystmt(hstmt,rc);
 
149
    }
 
150
    
 
151
    /* Free statement param resorces */
 
152
    rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
 
153
    mystmt(hstmt,rc);
 
154
    
 
155
    /* Free statement cursor resorces */
 
156
    rc = SQLFreeStmt(hstmt, SQL_CLOSE);
 
157
    mystmt(hstmt,rc);
 
158
 
 
159
    /* commit the transaction */
 
160
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
161
    mycon(hdbc,rc);
 
162
 
 
163
    /* Now fetch and verify the data */
 
164
    rc = SQLExecDirect(hstmt, "SELECT * FROM my_demo_param",SQL_NTS);
 
165
    mystmt(hstmt,rc);
 
166
 
 
167
    assert(10 == my_print_resultset(hstmt));
 
168
}
 
169
 
 
170
/********************************************************
 
171
* update data using parameters                          *
 
172
*********************************************************/
 
173
void my_param_update(SQLHDBC hdbc, SQLHSTMT hstmt)
 
174
{
 
175
  SQLRETURN   rc;
 
176
  SQLINTEGER  id=9, nRowCount;
 
177
  SQLCHAR     name[]="update";
 
178
 
 
179
  printf("\nmy_param_update:\n");
 
180
 
 
181
    /* prepare the insert statement with parameters */
 
182
    rc = SQLPrepare(hstmt,"UPDATE my_demo_param set name = ? WHERE id = ?",SQL_NTS);
 
183
    mystmt(hstmt,rc);
 
184
 
 
185
    /* now supply data to parameter 1 and 2 */    
 
186
    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, 
 
187
                          SQL_C_CHAR, SQL_CHAR, 0,0,
 
188
                          name, sizeof(name), NULL);
 
189
    mystmt(hstmt,rc);
 
190
 
 
191
    rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, 
 
192
                          SQL_C_LONG, SQL_INTEGER, 0,0,
 
193
                          &id, 0, NULL);
 
194
    mystmt(hstmt,rc);
 
195
 
 
196
    /* now execute the update statement */
 
197
    rc = SQLExecute(hstmt);
 
198
    mystmt(hstmt,rc);
 
199
    
 
200
    /* check the rows affected by the update statement */
 
201
    rc = SQLRowCount(hstmt, &nRowCount);
 
202
    mystmt(hstmt,rc);
 
203
    printf("\n total rows updated:%d\n",nRowCount);
 
204
    assert( nRowCount == 1);
 
205
    
 
206
    /* Free statement param resorces */
 
207
    rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
 
208
    mystmt(hstmt,rc);
 
209
    
 
210
    /* Free statement cursor resorces */
 
211
    rc = SQLFreeStmt(hstmt, SQL_CLOSE);
 
212
    mystmt(hstmt,rc);
 
213
 
 
214
    /* commit the transaction */
 
215
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
216
    mycon(hdbc,rc);
 
217
 
 
218
    /* Now fetch and verify the data */
 
219
    rc = SQLExecDirect(hstmt, "SELECT * FROM my_demo_param",SQL_NTS);
 
220
    mystmt(hstmt,rc);
 
221
 
 
222
    assert(10 == my_print_resultset(hstmt));
 
223
}
 
224
 
 
225
/********************************************************
 
226
* delete data using parameters                          *
 
227
*********************************************************/
 
228
void my_param_delete(SQLHDBC hdbc, SQLHSTMT hstmt)
 
229
{
 
230
  SQLRETURN   rc;
 
231
  SQLINTEGER  id, nRowCount;
 
232
 
 
233
  printf("\nmy_param_delete:\n");
 
234
    
 
235
    /* supply data to parameter 1 */    
 
236
    rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, 
 
237
                          SQL_C_LONG, SQL_INTEGER, 0,0,
 
238
                          &id, 0, NULL);
 
239
    mystmt(hstmt,rc);
 
240
 
 
241
    /* execute the DELETE STATEMENT to delete 5th row  */
 
242
    id = 5;
 
243
    rc = SQLExecDirect(hstmt,"DELETE FROM my_demo_param WHERE id = ?",SQL_NTS);
 
244
    mystmt(hstmt,rc);
 
245
    
 
246
    /* check the rows affected by the update statement */
 
247
    rc = SQLRowCount(hstmt, &nRowCount);
 
248
    mystmt(hstmt,rc);
 
249
    printf(" total rows deleted:%d\n",nRowCount);
 
250
    assert( nRowCount == 1);
 
251
 
 
252
    /* execute the DELETE STATEMENT to delete 8th row  */
 
253
    id = 8;
 
254
    rc = SQLExecDirect(hstmt,"DELETE FROM my_demo_param WHERE id = ?",SQL_NTS);
 
255
    mystmt(hstmt,rc);
 
256
    
 
257
    /* check the rows affected by the update statement */
 
258
    rc = SQLRowCount(hstmt, &nRowCount);
 
259
    mystmt(hstmt,rc);
 
260
    printf(" total rows deleted:%d\n",nRowCount);
 
261
    assert( nRowCount == 1);
 
262
    
 
263
    /* Free statement param resorces */
 
264
    rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
 
265
    mystmt(hstmt,rc);
 
266
    
 
267
    /* Free statement cursor resorces */
 
268
    rc = SQLFreeStmt(hstmt, SQL_CLOSE);
 
269
    mystmt(hstmt,rc);
 
270
 
 
271
    /* commit the transaction */
 
272
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
273
    mycon(hdbc,rc);
 
274
 
 
275
    /* Now fetch and verify the data */
 
276
    rc = SQLExecDirect(hstmt, "SELECT * FROM my_demo_param",SQL_NTS);
 
277
    mystmt(hstmt,rc);
 
278
 
 
279
    assert(8 == my_print_resultset(hstmt));
 
280
}
 
281
 
 
282
/********************************************************
 
283
* main routine                                          *
 
284
*********************************************************/
 
285
int main(int argc, char *argv[])
 
286
{
 
287
  SQLHENV    henv;
 
288
  SQLHDBC    hdbc; 
 
289
  SQLHSTMT   hstmt;
 
290
  SQLINTEGER narg;
 
291
  
 
292
    /*
 
293
     *  show the usage string when the user asks for this
 
294
    */    
 
295
      printf("***********************************************\n");
 
296
      printf("usage: my_param [DSN] [UID] [PWD] \n");  
 
297
      printf("***********************************************\n");     
 
298
 
 
299
    /*
 
300
     * if connection string supplied through arguments, overrite
 
301
     * the default one..
 
302
    */
 
303
    for(narg = 1; narg < argc; narg++)
 
304
    {     
 
305
      if ( narg == 1 )
 
306
        mydsn = argv[1];
 
307
      else if ( narg == 2 )
 
308
        myuid = argv[2];
 
309
      else if ( narg == 3 )
 
310
        mypwd = argv[3];      
 
311
    }   
 
312
 
 
313
    /* 
 
314
     * connect to MySQL server
 
315
    */
 
316
    myconnect(&henv,&hdbc,&hstmt); 
 
317
 
 
318
    /* 
 
319
     * initialize table
 
320
    */
 
321
    my_init_table(hdbc, hstmt);
 
322
 
 
323
    /* 
 
324
     * insert data using parameters
 
325
    */
 
326
    my_param_insert(hdbc, hstmt);
 
327
 
 
328
    /* 
 
329
     * parameter update 
 
330
    */
 
331
    my_param_update(hdbc, hstmt);
 
332
 
 
333
    /* 
 
334
     * parameter delete
 
335
    */
 
336
    my_param_delete(hdbc, hstmt);
 
337
 
 
338
    /* 
 
339
     * disconnect from the server, by freeing all resources
 
340
    */
 
341
    mydisconnect(&henv,&hdbc,&hstmt);
 
342
 
 
343
  return(0);
 
344
 
345
 
 
346
 
 
347