Learn about MySQL connector harvesting (including the latest documentation for the JDBC API) and some helpful tips for C + +

Source: Internet
Author: User
Tags documentation stmt

In order to write a look at the past of the database big job, spent my morning time to study MySQL C + + connector, probably understand some basic things, write to share. Here is the first piece of code I get started with: (before reading this article, if you haven't installed the newer c++/connect, please read my essay <vs2015 configure MySQL to make it work perfectly with C + + connection >). By the time this article was written, my connector version was: Mysql-connector-c++-8.0.11-windows-x86-64bit.

#include <stdlib.h>#include<iostream>#include"Mysql_connection.h"#include<cppconn/driver.h>#include<cppconn/exception.h>#include<cppconn/resultset.h>#include<cppconn/statement.h>using namespacestd;/*C + + connection MySQL step: ① declare four basic variables: sql::D river* driver;//Drive sql::connection* con;//connector sql::statement* stmt ;//Statement Object sql::resultset* ret;//result object ② Create a connection: Gets the driver instance that connects to the database on the host computer using the drive connection method, which returns a connector pointer. ③ creating a Statement object: Using a connector to create a statement ④ query results: Query with the Statement object's query (ExecuteQuery), return the result object pointer*/intMainintargcChar*argv[]) {cout<<Endl; cout<<"executing ' SELECT ' Hello World ' as _message ' ..."<<Endl; Try{sql::D River*driver; Sql::connection*con; Sql::statement*stmt; Sql::resultset*ret; //Create a connectionDriver =get_driver_instance (); Con= Driver->connect ("tcp://127.0.0.1:3306","Root","[email protected]"); //connect MySQL database Test        /*two ways: ①con = Driver->connect ("Tcp://127.0.0.1:3306/test", "Root", "[email protected]");          ②con = Driver->connect ("tcp://127.0.0.1:3306", "Root", "[email protected]");        Con->setschema ("test"); */Con->setschema ("Test"); stmt= con->createstatement (); //Insert a piece of data        stringMyStr ="INSERT into testuser values (26,\ "li\")"; intIquerycount = stmt->executeupdate (MYSTR); cout<<"The Update count is:"<< Iquerycount <<Endl; //Get query Resultsret = Stmt->executequery ("SELECT * from TestUser"); //ret = stmt->executequery ("Select ' Hello World ' as _message");        /*the next () function in resultset.h can get a tuple (row) in the result set each time, if one exists, and if you want to extract information from a column of that row, it can be obtained by means of a get prefix. */        //iterate through the result tuple         while(ret->Next ()) {cout<<"The ID is:"<< Ret->getint ("ID") <<"And the name is:"<< ret->getstring ("name") <<Endl; //cout << "\t...mysql replies:" << Endl;            ////Get a column property value by column name            //cout << ret->getstring ("_message") << Endl; //cout << "\t...mysql says it again:";            ////By Digital offset, 1 represents the first column            //cout << ret->getstring (1) << Endl;        }        //Finally, at the end of the query, always remember to release the memory in this order: result--statement--connector        Deleteret; Deletestmt; Deletecon; }    Catch(sql::sqlexception&e) {cout<<"# err:sqlexception in"<<__file__; cout<<"("<< __function__ <<")"<< __file__ <<Endl; cout<<"# Err:"<<E.what (); cout<<"(MySQL error code:"<<E.geterrorcode (); cout<<", SQLState:"<< e.getsqlstate () <<")"<<Endl; } cout<<Endl; System ("Pause"); returnexit_success;}/*compiler built-in macro extension: ANSI C standard in several standard scheduled macros: __line__: Insert the current source code line number in the source code; __FILE__: Insert the current source file name in the source file; __date__: Insert the current compilation date in the source file __time__ : Inserts the current compile time in the source file; __stdc__: When a program is required to strictly adhere to the ANSI C standard, the identity is assigned a value of 1;__cplusplus: The identifier is defined when writing a C + + program. */

The note above is clear, but the more useful thing is this: https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html It's the official JDBC API documentation, it's all great.

For those macros above, as a C + + programmer, that is quite useful ah, I wrote in the program notes often used in some of the macro and its meaning, but there is a I need to specifically explain, that is __function__, which is used to get the function name of the currently called function, how, very powerful, Here are the details:

just to get the name of the function, we embed a hard-coded string in the body of the function, which is tedious and prone to error, rather than looking at how to use the new C99 feature to get the function name when the program runs.

Object reflection libraries, debugging tools, and code analyzers often need to access the name of the function at run time, until recently, the only way to accomplish this task and portable is to manually embed a hard-coded string with the function name in the function body, needless to say, this method is very monotonous and easily leads to errors. This article will show you how to use the new C99 feature to get the function name at run time.

So how do you programmatically get the function name from the currently running function?

The answer is: use __FUNCTION__ and related macros. Elicit problems

In general, the most annoying phase of debugging is to constantly check whether a particular function has been called. The workaround for this problem is generally to add a cout or printf ()--If you use the C language as follows: void MyFunc () {cout<< "MyFunc ()" <<endl;//Other code}

Usually in a typical project, there will be thousands of functions, to add one such output statement in each function, it is undoubtedly sad to "Shushan" Ah, therefore, there is a need for a mechanism that can be done automatically to do this.

Get the name of a function

As a C + + programmer, you may often encounter macros such as __time__, __file__, and __date__, which are converted to strings that contain compile time, the name of the transformed unit to process, and the current time, at compile.

In the latest ISO C standard, as you know, C99 joins another useful, macro-like expression, __func__, which reports unmodified (that is, not cropped), the name of the function being accessed. Note that __func__ is not a macro because the preprocessor knows nothing about this function, instead it is implemented as an implicitly-declared constant character array:

Static Const Char " Function-name ";

At Function-name, for the actual function name. To activate this attribute, some compilers need to use a specific compilation flag, please review the appropriate compiler documentation for specific information.

With it, we can eliminate most of the drudgery of displaying function names by hand, and the above examples can be rewritten as follows:

void myfunc () {cout<<"__function__"<<endl;}

 

Official C99 standard the __func__ identifiers defined for this purpose are indeed worth attention, however, ISO C + + does not fully support all C99 extensions, so most compiler providers use __function__ instead, and __function__ Typically a macro defined as __func__, the name is used because it has been widely supported.

In Visual Studio 2005, this attribute is active by default, but cannot be used in conjunction with the/EP and/P compilation options. Please note that in an IDE environment, you cannot recognize __func__, but instead use __function__.

Comeau users should also use __function__, not __func__.  Users of C + + Builderx should use a slightly different name: __func__. GCC 3.0 and higher versions support both __func__ and __function__.

Once the current function name can be automatically obtained, you can define a function that displays any function name as follows:

void show_name (constChar * name) {cout<<name<<Endl;} void MyFunc () {show_name (__function__);} // Output: MyFunc void foo () {show_name (__function__);} // Output: Foo

Because __FUNCTION__ is initialized immediately after the function brace begins, the foo () and MyFunc () functions can be used safely in the argument list without worrying about overloading.

Signature and decorated name

The __function__ feature was originally designed for the C language, however, C + + programmers often needed additional information about their functions, and in Visual Studio 2005, two other non-standard extension features were supported: __funcdname__ and __funcsig __, which is translated into the decorated name and signature of a function, respectively. The decorated name of the function is useful, for example, when you want to check whether two compilers share the same ABI, and it can help you to decipher the ambiguous link errors and even invoke another C + + linked function from one DLL. In the following example, Show_name () reports the decorated name of the function: void MyFunc () {show_name (__funcdname__);//output: [email protected] @YAXXZ}

The signature of a function consists of a function name, a parameter list, a return type, and an included namespace. If it is a member function, its class name and Const/volatile qualifier will also be part of the signature. The following code shows the difference between a separate function and a const member function signature, with the names, return types, and parameters of the two functions identical:

void // void __cdecl myfunc (void) void Const // void __thiscall s::myfunc (void) const;

__FUNCTION__ 's original text please see here: http://blog.sina.com.cn/s/blog_65d6476a0101aa4t.html

Learn about MySQL connector harvesting (including the latest documentation for the JDBC API) and some helpful tips for C + +

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.