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

« back to all changes in this revision

Viewing changes to Drivers/MySQL/samples/my_basics.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_basics.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 the basic execution of SQL       *
 
21
 *  statements using  MySQL ODBC 3.51 driver                               *
 
22
 *                                                                         *
 
23
 ***************************************************************************/
 
24
 
 
25
#include "my_utility.h" /* MyODBC 3.51 sample utility header */
 
26
 
 
27
/********************************************************
 
28
* Execution of BASIC SQL statements                     *
 
29
*********************************************************/
 
30
void my_basics(SQLHDBC hdbc, SQLHSTMT hstmt)
 
31
{
 
32
  SQLRETURN  rc;
 
33
  SQLINTEGER nRowCount;
 
34
 
 
35
  printf("\nmy_basics:\n");
 
36
 
 
37
    /* drop table 'myodbc3_demo_basic' if it already exists */
 
38
    rc = SQLExecDirect(hstmt,"DROP TABLE if exists myodbc3_demo_basic",SQL_NTS);
 
39
    mystmt(hstmt,rc);
 
40
 
 
41
    /* commit the transaction */
 
42
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
43
    mycon(hdbc,rc);
 
44
 
 
45
    /* create the table 'myodbc3_demo_result' */
 
46
    rc = SQLExecDirect(hstmt,"CREATE TABLE myodbc3_demo_basic(\
 
47
                              id int primary key auto_increment,\
 
48
                              name varchar(20))",SQL_NTS);
 
49
    mystmt(hstmt,rc);
 
50
 
 
51
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
 
52
    mycon(hdbc,rc);    
 
53
 
 
54
    /* insert 3 rows of data */    
 
55
    rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_basic values(\
 
56
                              1,'MySQL')",SQL_NTS);
 
57
    mystmt(hstmt,rc);
 
58
    
 
59
    rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_basic values(\
 
60
                              2,'MyODBC')",SQL_NTS);
 
61
    mystmt(hstmt,rc);
 
62
    
 
63
    rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_basic values(\
 
64
                              3,'monty')",SQL_NTS);
 
65
    mystmt(hstmt,rc);
 
66
 
 
67
    /* commit the transaction */
 
68
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
69
    mycon(hdbc,rc);
 
70
 
 
71
    /* update second row */    
 
72
    rc = SQLExecDirect(hstmt,"UPDATE myodbc3_demo_basic set name=\
 
73
                              'MyODBC 3.51' where id=2",SQL_NTS);
 
74
    mystmt(hstmt,rc);
 
75
 
 
76
    /* get the rows affected by update statement */
 
77
    rc = SQLRowCount(hstmt,&nRowCount);
 
78
    mystmt(hstmt,rc);
 
79
    printf(" total rows updated:%d",nRowCount);
 
80
 
 
81
    /* commit the transaction */
 
82
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
83
    mycon(hdbc,rc);
 
84
 
 
85
    /* delete third column */
 
86
    rc = SQLExecDirect(hstmt,"DELETE FROM myodbc3_demo_basic where id = 3",SQL_NTS);
 
87
    mystmt(hstmt,rc);
 
88
 
 
89
    /* get the rows affected by delete statement */
 
90
    rc = SQLRowCount(hstmt,&nRowCount);
 
91
    mystmt(hstmt,rc);
 
92
    printf(" total rows deleted:%d",nRowCount);
 
93
 
 
94
    /* commit the transaction */
 
95
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
96
    mycon(hdbc,rc);
 
97
 
 
98
    /* alter the table 'myodbc3_demo_basic' to 'myodbc3_new_name' */
 
99
    rc = SQLExecDirect(hstmt,"ALTER TABLE myodbc3_demo_basic RENAME myodbc3_new_name",SQL_NTS);
 
100
    mystmt(hstmt,rc);
 
101
 
 
102
    /* commit the transaction */
 
103
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
104
    mycon(hdbc,rc);
 
105
 
 
106
    /* drop the table with the original table name, and it should
 
107
       return error saying 'table not found'
 
108
    */
 
109
    rc = SQLExecDirect(hstmt,"DROP TABLE myodbc3_demo_basic",SQL_NTS);
 
110
    mystmt_err(hstmt, rc == SQL_ERROR, rc);
 
111
 
 
112
    /* now drop the table, which is altered..*/   
 
113
    rc = SQLExecDirect(hstmt,"DROP TABLE myodbc3_new_name",SQL_NTS);
 
114
    mystmt(hstmt,rc);
 
115
 
 
116
    /* commit the transaction */
 
117
    rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); 
 
118
    mycon(hdbc,rc);
 
119
 
 
120
    /* free the statement cursor */
 
121
    rc = SQLFreeStmt(hstmt, SQL_CLOSE);
 
122
    mystmt(hstmt,rc);
 
123
 
 
124
    printf(" success!!\n");
 
125
}
 
126
/********************************************************
 
127
* main routine                                          *
 
128
*********************************************************/
 
129
int main(int argc, char *argv[])
 
130
{
 
131
  SQLHENV    henv;
 
132
  SQLHDBC    hdbc; 
 
133
  SQLHSTMT   hstmt;
 
134
  SQLINTEGER narg;
 
135
  
 
136
    /*
 
137
     *  show the usage string when the user asks for this
 
138
    */    
 
139
      printf("***********************************************\n");
 
140
      printf("usage: my_basics [DSN] [UID] [PWD] \n");  
 
141
      printf("***********************************************\n");          
 
142
 
 
143
    /*
 
144
     * if connection string supplied through arguments, overrite
 
145
     * the default one..
 
146
    */
 
147
    for(narg = 1; narg < argc; narg++)
 
148
    {     
 
149
      if ( narg == 1 )
 
150
        mydsn = argv[1];
 
151
      else if ( narg == 2 )
 
152
        myuid = argv[2];
 
153
      else if ( narg == 3 )
 
154
        mypwd = argv[3];      
 
155
    }   
 
156
 
 
157
    /* 
 
158
     * connect to MySQL server
 
159
    */
 
160
    myconnect(&henv,&hdbc,&hstmt); 
 
161
 
 
162
    /* 
 
163
     * simple execution of SQL statements
 
164
    */
 
165
    my_basics(hdbc, hstmt);
 
166
 
 
167
    /* 
 
168
     * disconnect from the server, by freeing all resources
 
169
    */
 
170
    mydisconnect(&henv,&hdbc,&hstmt);
 
171
 
 
172
  return(0);
 
173
 
174
 
 
175
 
 
176