1
/***************************************************************************
2
my_basics.c - description
5
copyright : (C) MySQL AB 1995-2002, www.mysql.com
6
author : venu ( venu@mysql.com )
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
18
/***************************************************************************
20
* This is a basic sample to demonstrate the basic execution of SQL *
21
* statements using MySQL ODBC 3.51 driver *
23
***************************************************************************/
25
#include "my_utility.h" /* MyODBC 3.51 sample utility header */
27
/********************************************************
28
* Execution of BASIC SQL statements *
29
*********************************************************/
30
void my_basics(SQLHDBC hdbc, SQLHSTMT hstmt)
35
printf("\nmy_basics:\n");
37
/* drop table 'myodbc3_demo_basic' if it already exists */
38
rc = SQLExecDirect(hstmt,"DROP TABLE if exists myodbc3_demo_basic",SQL_NTS);
41
/* commit the transaction */
42
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
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);
51
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
54
/* insert 3 rows of data */
55
rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_basic values(\
59
rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_basic values(\
60
2,'MyODBC')",SQL_NTS);
63
rc = SQLExecDirect(hstmt,"INSERT INTO myodbc3_demo_basic values(\
67
/* commit the transaction */
68
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
71
/* update second row */
72
rc = SQLExecDirect(hstmt,"UPDATE myodbc3_demo_basic set name=\
73
'MyODBC 3.51' where id=2",SQL_NTS);
76
/* get the rows affected by update statement */
77
rc = SQLRowCount(hstmt,&nRowCount);
79
printf(" total rows updated:%d",nRowCount);
81
/* commit the transaction */
82
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
85
/* delete third column */
86
rc = SQLExecDirect(hstmt,"DELETE FROM myodbc3_demo_basic where id = 3",SQL_NTS);
89
/* get the rows affected by delete statement */
90
rc = SQLRowCount(hstmt,&nRowCount);
92
printf(" total rows deleted:%d",nRowCount);
94
/* commit the transaction */
95
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
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);
102
/* commit the transaction */
103
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
106
/* drop the table with the original table name, and it should
107
return error saying 'table not found'
109
rc = SQLExecDirect(hstmt,"DROP TABLE myodbc3_demo_basic",SQL_NTS);
110
mystmt_err(hstmt, rc == SQL_ERROR, rc);
112
/* now drop the table, which is altered..*/
113
rc = SQLExecDirect(hstmt,"DROP TABLE myodbc3_new_name",SQL_NTS);
116
/* commit the transaction */
117
rc = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
120
/* free the statement cursor */
121
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
124
printf(" success!!\n");
126
/********************************************************
128
*********************************************************/
129
int main(int argc, char *argv[])
137
* show the usage string when the user asks for this
139
printf("***********************************************\n");
140
printf("usage: my_basics [DSN] [UID] [PWD] \n");
141
printf("***********************************************\n");
144
* if connection string supplied through arguments, overrite
147
for(narg = 1; narg < argc; narg++)
151
else if ( narg == 2 )
153
else if ( narg == 3 )
158
* connect to MySQL server
160
myconnect(&henv,&hdbc,&hstmt);
163
* simple execution of SQL statements
165
my_basics(hdbc, hstmt);
168
* disconnect from the server, by freeing all resources
170
mydisconnect(&henv,&hdbc,&hstmt);