Mysql entry-level series: MYSQL client program 2-Added error check [group chart] _ MySQL

Source: Internet
Author: User
Mysql entry-level series: MYSQL client program 2-Added error check [group chart] 6.3 Client Program 2-Added error check

Our second client program will be the same as the first client program, but will modify them to consider the possibility of errors. Projects such as "checking errors as exercises for readers" are quite common in programming literature, probably because checking errors are rather annoying. However, I agree that the MySQL client program should test the error conditions.

And respond appropriately. For some reason, the client library that returns the status value is called to do these tasks, and you have to bear the consequences of ignoring them. In the end, you still need to try to capture errors in the program because there is no error check. users of these programs will be surprised that the program runs so irregularly. Consider our program, client program 1. How do I know if it is actually connected to the server? You can view the server logs to find the Connect and Quit events corresponding to the running program time:

This message indicates that no connection is created. Unfortunately, client 1 does not tell us the results. Actually, it cannot. It cannot implement any error check, so it does not even know what happened. In any case, of course, you may not have to check the log to find whether the server can be connected! Let's immediately correct it. The routines that return values in the MySQL client library basically indicate success or failure in either of the following two ways:

■ When the operation succeeds, the pointer function of the value returns a non-NULL pointer. if the operation fails, NULL is returned (here, NULL indicates "c null pointer" instead of "MySQLNULL column value "). So far, the routines mysql_init () and mysql_real_connect () of the client library we use indicate success by returning the pointer of the connection handler, and NULL indicate failure.

■ A function with an integer value generally returns 0 if it is successful, and a non-0 value if it fails. Do not test specific non-0 values, such as-1. When a failure occurs, the client library function does not guarantee that any specific value is returned. Sometimes, you may see code like the following older errors to test the return value:

This test may or may not work. MySQLAPI does not specify any non-zero error return as a specific value, but only determines whether it (apparently) is 0. This test should be written into one of the following two sections:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.