Mysql API / C/C++ 연동하기 강좌 (5. mysql_field_count() 깔끔한 Error처리)
unsigned int mysql_field_count(MYSQL *mysql)
Description
Returns the number of columns for the most recent query on the connection.
The normal use of this function is when mysql_store_result()
returned NULL
(and thus you have no result set pointer). In this case, you can call mysql_field_count()
to determine whether mysql_store_result()
should have produced a nonempty result. This allows the client program to take proper action without knowing whether the query was a SELECT
(or SELECT
-like) statement. The example shown here illustrates how this may be done.
Return Values
An unsigned integer representing the number of columns in a result set.
Errors
None.
Example
An alternative is to replace the
123456789101112131415161718192021222324252627282930 MYSQL_RES *result;unsigned int num_fields;unsigned int num_rows;if (mysql_query(&mysql,query_string)){// error}else // query succeeded, process any data returned by it{result = mysql_store_result(&mysql);if (result) // there are rows{num_fields = mysql_num_fields(result);// retrieve rows, then call mysql_free_result(result)}else // mysql_store_result() returned nothing; should it have?{if(mysql_field_count(&mysql) == 0){// query does not return data// (it was not a SELECT)num_rows = mysql_affected_rows(&mysql);}else // mysql_store_result() should have returned data{fprintf(stderr, "Error: %s\n", mysql_error(&mysql));}}}
mysql_field_count(&mysql)
call with mysql_errno(&mysql)
. In this case, you are checking directly for an error from mysql_store_result()
rather than inferring from the value of mysql_field_count()
whether the statement was a SELECT
. 깔끔하게 에러가 처리된다....
출처 : MySQL 5.1 Reference Manual :: 21 Connectors and APIs :: 21.9 MySQL C API :: 21.9.3 C API Function Descriptions :: 21.9.3.22 mysql_field_count()
'프로그래밍 언어 > C++' 카테고리의 다른 글
Mysql API / C/C++ 연동하기 강좌 (7. ODBC를 이용한 mysql 연동하기) (5) | 2009.12.17 |
---|---|
Mysql API / C/C++ 연동하기 강좌 (6. mysql c++클래스 연동 예제 / 도서관리) (4) | 2009.12.15 |
Mysql API / C/C++ 연동하기 강좌 (4. C/C++ Mysql Class 소스) (0) | 2009.12.04 |
Mysql API / C/C++ 연동하기 강좌 (3. C/C++ Mysql 테스트 Full소스) (2) | 2009.12.02 |
Mysql API / C/C++ 연동하기 강좌 (2. C/C++ Mysql API 알아보기) (0) | 2009.12.02 |