- 浏览: 1521988 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (532)
- 软件设计师 (7)
- PSP (5)
- NET MD (9)
- Hibernate (8)
- DIY (51)
- Work (43)
- GAME (24)
- 未分类 (44)
- iPod (6)
- MySQL (39)
- JSP (7)
- 日语能力考试 (36)
- 小说 (4)
- 豆包网 (23)
- 家用电脑 (7)
- DB2 (36)
- C/C++ (18)
- baby (9)
- Linux (13)
- thinkpad (23)
- OA (1)
- UML (6)
- oracle (24)
- 系统集成 (27)
- 脑梗塞 (6)
- 车 (8)
- MainFrame (8)
- Windows 7 (13)
- 手机 (8)
- git (12)
- AHK (2)
- COBOL (2)
- Java (9)
最新评论
-
安静听歌:
... ...
UUID做主键,好还是不好?这是个问题。 -
lehehe:
http://www.haoservice.com/docs/ ...
天气预报 -
lehehe:
[url http://www.haoservice.com/ ...
天气预报 -
liubang201010:
监控TUXEDO 的软件推荐用这个,专业,权威.并能提供报警和 ...
(转载)Tuxedo中间件简介 -
tinkame:
Next[j] =-1 当j=0时;=Max{k|0<k ...
KMP字符串模式匹配详解
示例:交互式 SQL 和等价的 DB2 UDB CLI 函数调用
此示例显示交互式 SQL 语句的执行,并遵循编写 DB2 UDB CLI 应用程序中描述的流程。
有关代码示例的信息,请参阅代码免责信息。
/************************************************************************* ** file = typical.c ** ** Example of executing interactive SQL statements, displaying result sets ** and simple transaction management. ** ** Functions used: ** ** SQLAllocConnect SQLFreeConnect ** SQLAllocEnv SQLFreeEnv ** SQLAllocStmt SQLFreeStmt ** SQLConnect SQLDisconnect ** ** SQLBindCol SQLFetch ** SQLDescribeCol SQLNumResultCols ** SQLError SQLRowCount ** SQLExecDirect SQLTransact ** **************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h> #include "sqlcli.h" #define MAX_STMT_LEN 255 #define MAXCOLS 100 #define max(a,b) (a > b ? a : b) int initialize(SQLHENV *henv, SQLHDBC *hdbc); int process_stmt(SQLHENV henv, SQLHDBC hdbc, SQLCHAR *sqlstr); int terminate(SQLHENV henv, SQLHDBC hdbc); int print_error(SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt); int check_error(SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, SQLRETURN frc); void display_results(SQLHSTMT hstmt, SQLSMALLINT nresultcols); /******************************************************************* ** main ** - initialize ** - start a transaction ** - get statement ** - another statement? ** - COMMIT or ROLLBACK ** - another transaction? ** - terminate *******************************************************************/ int main() { SQLHENV henv; SQLHDBC hdbc; SQLCHAR sqlstmt[MAX_STMT_LEN + 1]=""; SQLCHAR sqltrans[sizeof("ROLLBACK")]; SQLRETURN rc; rc = initialize(&henv, &hdbc); if (rc == SQL_ERROR) return(terminate(henv, hdbc)); printf("Enter an SQL statement to start a transaction(or 'q' to Quit):\n"); gets(sqlstmt); while (sqlstmt[0] !='q') { while (sqlstmt[0] != 'q') { rc = process_stmt(henv, hdbc, sqlstmt); if (rc == SQL_ERROR) return(SQL_ERROR); printf("Enter an SQL statement(or 'q' to Quit):\n"); gets(sqlstmt); } printf("Enter 'c' to COMMIT or 'r' to ROLLBACK the transaction\n"); fgets(sqltrans, sizeof("ROLLBACK"), stdin); if (sqltrans[0] == 'c') { rc = SQLTransact (henv, hdbc, SQL_COMMIT); if (rc == SQL_SUCCESS) printf ("Transaction commit was successful\n"); else check_error (henv, hdbc, SQL_NULL_HSTMT, rc); } if (sqltrans[0] == 'r') { rc = SQLTransact (henv, hdbc, SQL_ROLLBACK); if (rc == SQL_SUCCESS) printf ("Transaction roll back was successful\n"); else check_error (henv, hdbc, SQL_NULL_HSTMT, rc); } printf("Enter an SQL statement to start a transaction or 'q' to quit\n"); gets(sqlstmt); } terminate(henv, hdbc); return (SQL_SUCCESS); }/* end main */ /******************************************************************* ** process_stmt ** - allocates a statement handle ** - executes the statement ** - determines the type of statement ** - if there are no result columns, therefore non-select statement ** - if rowcount > 0, assume statement was UPDATE, INSERT, DELETE ** else ** - assume a DDL, or Grant/Revoke statement ** else ** - must be a select statement. ** - display results ** - frees the statement handle *******************************************************************/ int process_stmt (SQLHENV henv, SQLHDBC hdbc, SQLCHAR *sqlstr) { SQLHSTMT hstmt; SQLSMALLINT nresultcols; SQLINTEGER rowcount; SQLRETURN rc; SQLAllocStmt (hdbc, &hstmt); /* allocate a statement handle */ /* execute the SQL statement in "sqlstr" */ rc = SQLExecDirect (hstmt, sqlstr, SQL_NTS); if (rc != SQL_SUCCESS) if (rc == SQL_NO_DATA_FOUND) { printf("\nStatement executed without error, however,\n"); printf("no data was found or modified\n"); return (SQL_SUCCESS); } else check_error (henv, hdbc, hstmt, rc); SQLRowCount (hstmt, &rowcount); rc = SQLNumResultCols (hstmt, &nresultcols); if (rc != SQL_SUCCESS) check_error (henv, hdbc, hstmt, rc); /* determine statement type */ if (nresultcols == 0) /* statement is not a select statement */ { if (rowcount > 0 ) /* assume statement is UPDATE, INSERT, DELETE */ { printf ("Statement executed, %ld rows affected\n", rowcount); } else /* assume statement is GRANT, REVOKE or a DLL statement */ { printf ("Statement completed successful\n"); } } else /* display the result set */ { display_results(hstmt, nresultcols); } /* end determine statement type */ SQLFreeStmt (hstmt, SQL_DROP ); /* free statement handle */ return (0); }/* end process_stmt */ /******************************************************************* ** initialize ** - allocate environment handle ** - allocate connection handle ** - prompt for server, user id, & password ** - connect to server *******************************************************************/ int initialize(SQLHENV *henv, SQLHDBC *hdbc) { SQLCHAR server[18], uid[10], pwd[10]; SQLRETURN rc; rc = SQLAllocEnv (henv); /* allocate an environment handle */ if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); rc = SQLAllocConnect (*henv, hdbc); /* allocate a connection handle */ if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); printf("Enter Server Name:\n"); gets(server); printf("Enter User Name:\n"); gets(uid); printf("Enter Password Name:\n"); gets(pwd); if (uid[0] == '\0') { rc = SQLConnect (*hdbc, server, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS); if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); } else { rc = SQLConnect (*hdbc, server, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS); if (rc != SQL_SUCCESS ) check_error (*henv, *hdbc, SQL_NULL_HSTMT, rc); } }/* end initialize */ /******************************************************************* ** terminate ** - disconnect ** - free connection handle ** - free environment handle *******************************************************************/ int terminate(SQLHENV henv, SQLHDBC hdbc) { SQLRETURN rc; rc = SQLDisconnect (hdbc); /* disconnect from database */ if (rc != SQL_SUCCESS ) print_error (henv, hdbc, SQL_NULL_HSTMT); rc = SQLFreeConnect (hdbc); /* free connection handle */ if (rc != SQL_SUCCESS ) print_error (henv, hdbc, SQL_NULL_HSTMT); rc = SQLFreeEnv (henv); /* free environment handle */ if (rc != SQL_SUCCESS ) print_error (henv, SQL_NULL_HDBC, SQL_NULL_HSTMT); }/* end terminate */ /******************************************************************* ** display_results - displays the selected character fields ** ** - for each column ** - get column name ** - bind column ** - display column headings ** - fetch each row ** - if value truncated, build error message ** - if column null, set value to "NULL" ** - display row ** - print truncation message ** - free local storage ** *******************************************************************/ void display_results(SQLHSTMT hstmt, SQLSMALLINT nresultcols) { SQLCHAR colname[32]; SQLSMALLINT coltype[MAXCOLS]; SQLSMALLINT colnamelen; SQLSMALLINT nullable; SQLINTEGER collen[MAXCOLS]; SQLSMALLINT scale; SQLINTEGER outlen[MAXCOLS]; SQLCHAR * data[MAXCOLS]; SQLCHAR errmsg[256]; SQLRETURN rc; SQLINTEGER i; SQLINTEGER displaysize; for (i = 0; i < nresultcols; i++) { SQLDescribeCol (hstmt, i+1, colname, sizeof (colname), &colnamelen, &coltype[i], &collen[i], &scale, &nullable); /* get display length for column */ SQLColAttributes (hstmt, i+1, SQL_DESC_PRECISION, NULL, 0 , NULL, &displaysize); /* set column length to max of display length, and column name length. Plus one byte for null terminator */ collen[i] = max(displaysize, collen[i]); collen[i] = max(collen[i], strlen((char *) colname) ) + 1; printf ("%-*.*s", collen[i], collen[i], colname); /* allocate memory to bind column */ data[i] = (SQLCHAR *) malloc (collen[i]); /* bind columns to program vars, converting all types to CHAR */ SQLBindCol (hstmt, i+1, SQL_C_CHAR, data[i], collen[i], &outlen[i]); } printf("\n"); /* display result rows */ while ((rc = SQLFetch (hstmt)) != SQL_NO_DATA_FOUND) { errmsg[0] = '\0'; for (i = 0; i < nresultcols; i++) { /* Build a truncation message for any columns truncated */ if (outlen[i] >= collen[i]) { sprintf ((char *) errmsg + strlen ((char *) errmsg), "%d chars truncated, col %d\n", outlen[i]-collen[i]+1, i+1); } if (outlen[i] == SQL_NULL_DATA) printf ("%-*.*s", collen[i], collen[i], "NULL"); else printf ("%-*.*s", collen[i], collen[i], data[i]); } /* for all columns in this row */ printf ("\n%s", errmsg); /* print any truncation messages */ } /* while rows to fetch */ /* free data buffers */ for (i = 0; i < nresultcols; i++) { free (data[i]); } }/* end display_results /******************************************************************* ** SUPPORT FUNCTIONS ** - print_error - call SQLError(), display SQLSTATE and message ** - check_error - call print_error ** - check severity of Return Code ** - rollback & exit if error, continue if warning *******************************************************************/ /*******************************************************************/ int print_error (SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt) { SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1]; SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; SQLINTEGER sqlcode; SQLSMALLINT length; while ( SQLError(henv, hdbc, hstmt, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length) == SQL_SUCCESS ) { printf("\n **** ERROR *****\n"); printf(" SQLSTATE: %s\n", sqlstate); printf("Native Error Code: %ld\n", sqlcode); printf("%s \n", buffer); }; return; } /*******************************************************************/ int check_error (SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, SQLRETURN frc) { SQLRETURN rc; print_error(henv, hdbc, hstmt); switch (frc){ case SQL_SUCCESS : break; case SQL_ERROR : case SQL_INVALID_HANDLE: printf("\n ** FATAL ERROR, Attempting to rollback transaction **\n"); rc = SQLTransact(henv, hdbc, SQL_ROLLBACK); if (rc != SQL_SUCCESS) printf("Rollback Failed, Exiting application\n"); else printf("Rollback Successful, Exiting application\n"); terminate(henv, hdbc); exit(frc); break; case SQL_SUCCESS_WITH_INFO : printf("\n ** Warning Message, application continuing\n"); break; case SQL_NO_DATA_FOUND : printf("\n ** No Data Found ** \n"); break; default : printf("\n ** Invalid Return Code ** \n"); printf(" ** Attempting to rollback transaction **\n"); SQLTransact(henv, hdbc, SQL_ROLLBACK); terminate(henv, hdbc); exit(frc); break; } return(SQL_SUCCESS); }
发表评论
-
(转)浅谈IBM DB2的数据库备份与恢复
2011-11-23 16:41 1805http://bzhang.mild.blog.163.com ... -
(转)DB2备份恢复数据库步骤
2011-11-23 16:23 910http://tech.it168.com/ibmtec ... -
(转)DB2 SQL Error: SQLCODE=-964, SQLSTATE=57011的原因及解决方法
2011-11-07 15:03 9452http://blog.sina.com.cn/s ... -
(转)DB2解决“数据库日志已满”操作 SQLCODE=-964
2011-11-07 14:49 2643http://www.db2china.ne ... -
(转)用哪个DB2命令查看DB2 instance是启动还是停止的?
2011-09-30 13:25 3609http://bbs.chinaunix.net/thre ... -
(转)论 LOAD 与 IMPORT 中的 codepage 转换
2011-07-11 14:55 1119http://www.ibm.com/develo ... -
(转)DB2导出数据库表结构和数据
2011-07-11 14:19 1517http://hi.baidu.com/mylovecha ... -
(转)DB2 9 中基于字符的字符串函数
2011-06-16 11:32 1389http://www.ibm.com/develo ... -
(转)db2look:生成 DDL 以便重新创建在数据库中定义的对象
2011-05-31 14:56 1610http://blog.csdn.net/hrfdotnet/ ... -
(转)【俊哥儿张】DB2:学习 DB2LOOK 命令
2011-05-31 14:54 1337http://space.itpub.net/770528 ... -
(转)Recreate optimizer access plans using db2look
2011-05-31 14:53 1238http://www.ibm.com/develo ... -
(转)DB2中ALTER TABLE为什么需要REORG操作?
2011-05-12 13:10 1632http://www.flatws.cn/article/ ... -
(转)DB2 3.2.2 表空间维护
2011-05-11 12:53 1146http://book.51cto.com/art/2 ... -
(转)DB2 数据类型
2011-05-11 12:52 3952http://hi.baidu.com/uvvv/blog ... -
(转)DB2 Basics: Table spaces and buffer pools
2011-05-06 15:29 1427http://www.ibm.com/develo ... -
(转)SQL1092N "USER" does not have the authority to perform therequested command -
2011-03-23 17:39 5426http://dbaspot.com/foru ... -
(转)DB2 sqlstate 57016 原因码 "7"错误详解
2011-03-07 17:38 1459http://www.db2china.net/clu ... -
(转)在英语 OS 上设置 DB2 UDB 版本 8 中的混合字节字符集(MBCS)数据库
2011-02-16 16:25 1560http://www.ibm.com/develo ... -
(转)DB2 CODEPAGE List
2011-02-15 15:15 1378http://www.db2china.net/hom ... -
(转)快速参考: DB2 命令行处理器(CLP)中的常用命令
2010-11-11 10:44 941http://www.ibm.com/develo ...
相关推荐
IBM DB2 UDB 词汇表.zip IBM DB2 UDB 词汇表.zip
DB2 UDB安全模型主要包括两部分:身份验证和授权。身份验证就是使用安全机制验证所提供用户ID和口令的过程。用户和组身份验证由DB2 UDB外部的设施管理,比如操作系统、域控制器或者Kerberos安全系统。这和其他数据库...
微软环境DB2 UDB 7.1开发指南 微软环境DB2 UDB 7.1开发指南
DB2 UDB SQL入门,经典
DB2 Universal Database (UDB) Express Edition 是针对中小企业的关系数据库管理系统(Relational ...DB2 UDB Express 运行于 Linux 和 Windows 之上,至多可支持两个处理器和一些特性,具备自调优和自配置功能。
DB2 UDB 9.1 For AIX 安装指南,文档思路清晰,一看就会。
作者 Paul Zikopoulos 通过一个对比列表,让读者可以轻松地理解分布式 IBM:registered: DB2:registered: Universal Database:trade_mark: (DB2 UDB) 服务器家族各成员之间,在基本许可规则、功能以及特性等方面的...
支持10个主要的数据库系统:Oracle, SQL Server, DB2 UDB, DB2 for iSeries, Sybase ASE, Sybase SQL Anywhere, Postgre SQL, Amazon Redshift, MySQL, Microsoft Access等等 1、拥有可视SQL查询生成器 2、可轻松...
IBM的Oracle to DB2 Udb Conversion Guide ,英文的,希望对大家有帮助
调优 DB2 UDB 调优 DB2 UDB v8.1 及其数据库的最佳实践
DB2 UDB培训资料 DB2 UDB培训资料
DB2 UDB 内存模型
DB2 UDB SQL语句的生命周期.pdf
本文提供了一系列关于 DB2 UDB for Linux, UNIX:registered: 和 Windows:registered: 的在线学习资料。此外,要查看这类信息,DB2 UDB Information Center 也是一个好去处。本文列出了很多项目,有的作为文章发表在 ...
db2_udb_v8开发文档
MySQL to DB2 UDB Conversion Guide
DB2 UDB OLTP 调优资料
DB2 通用数据库的英文原版教程9.1版。