MySQL Learning notes

Source: Internet
Author: User
Tags error code access database mysql database

Linux under C++/C connection MySQL database (i)

First, the preparation before the connection

Raw Material: Ubuntu12.04lts

(MySQL5.5 or more advanced versions, new packages, gcc/g++ or codeblosks compilers are already installed)

After installing the above package, we can use MySQL to complete the data management work, but many times we need to write programs to access MySQL. At this point, the MySQL driver header file needs to be loaded in the program, but the default driver pack is not installed, so we need to find the "Libmysqld-dev" package in the new stand and install it.

After the installation is complete, in the "/usr/include/mysql/" directory will have a lot of corresponding header files, such as mysql.h.

When you write a program, you need to compile it:

1, if you choose to use gcc/g++ to complete the command line, you need to add the-lmysqlclient option. Such as:

?

1 g++-lmysqlclient Mysql_test.cc-o Mysqltest

2, if the selection is codeblocks, you need to set the connection library to mysqlclient in the build option.

Start compiling.

Analysis of header file and correlation function

The functions listed in this blog are all included in the header file , so you need to add a line at the beginning of the program writing:

?

1 #include <mysql/mysql.h>

1, with CAPI connection MySQL database has two steps:

1 initialization of a connection handle

2) Establish the connection

The functions used are as follows:

?

1 2 3 4 5 6 7 8 9 of MySQL *mysql_init (MySQL *connection);               //initialization of connection handle//successful return of MySQL structure pointer, failure return NULL   mysql *mysql_real_connect (MySQL *connection,                              const Char *server_host,                                                       const Char *sql_user_name,                       &Nbsp;     const Char *sql_password,                               const Char *db_name,                              unsigned int Port_ number,                              const Char *unix_socket_name,                              unsigned int flags); //Establish connection//successfully return MySQL structure pointer, failure return NULL

2, MySQL connection work is completed, the need for the MySQL connection to release

?

1 2 void Mysql_close (MySQL *connection); Close connection//parameter connection is emptied and the pointer becomes invalid

3. Mysql_options is used to set additional options and affect the connection behavior, but it can only be invoked between Mysql_init and Mysql_real_connect

?

1 int mysql_options (MySQL *connection, enum mysql_option option, const char *argument); Setting connection options

The value of option is one of the following four values:

?

1 2 3 4 The command that will be executed when mysql_init_command//connected to the MySQL server is automatically executed again when the connection is reconnected. Wait seconds before mysql_opt_connect_timeout//connection timeout mysql_opt_compress//network connection Use compression mechanism Mysql_opt_protocol//protocol type to use, shadow One of the mysql_protocol_type enumerated values defined in the view mysql.h

4. The above function is to run under the assumption that there is no error, if there is an error in the database connection process, we need the following two functions to handle the error message:

?

1 2 unsigned int mysql_errno (MySQL *connection);           Returns the error code (0 indicates an error) char *mysql_error (MySQL *connection); Returns the error message, which is a null-terminated string

5, the Program Access database is not only to establish a connection with the database, more importantly, through the execution of SQL statements query or change the data in the database. Executing SQL statements can be implemented through the following functions.

?

1 int mysql_query (MySQL *connection, const char *query);

The SQL statement is divided into two classes, which return the SQL of the data and the SQL that does not return data (UPDATE, DELETE, INSERT).

Here we only describe the SQL that does not return data

?

1 2 My_ulonglong mysql_affected_rows (MySQL *connection); Returns the rows affected by the query after the execution of a SQL statement, you can determine whether the execution of SQL succeeds by calling this function to see the number of rows of data state changes in the database after the execution of the SQL statements.

Iii. examples

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72-73 #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cstdlib&gt; #include &lt;mysql/mysql.h&gt; &nbsp; using namespace Std; &nbsp; void mysql_err_function (MySQL * connection); &nbsp; int main () {&nbsp;&nbsp;&nbsp;&nbsp;//freopen ("Input.txt", "R", stdin); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;mysql * Connection &nbsp;&nbsp;&nbsp;&nbsp;connection = Mysql_init (NULL); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if (!connection) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;cout &lt;&lt; "Mysql_init failed!" &lt;&lt; Endl; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit (-1); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if (!mysql_real_connect (Connection, "localhost", "root", " 123456 "," Test ", 0,null,0) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; "Connection to MySQL failed!" &lt;&lt; Endl; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mysql_err_function (connection); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp; &nbsp;&Nbsp;&nbsp;&nbsp;cout &lt;&lt; "Connection to MySQL Server is Success ..." &lt;&lt; Endl; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;string str; &nbsp;&nbsp;&nbsp;&nbsp;getline (CIN,STR); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;int res = 0; &nbsp;&nbsp;&nbsp;&nbsp;int affected_count = 0; &nbsp;&nbsp;&nbsp;&nbsp;while (str!= "close" &amp;&amp; str!= "" &amp;&amp;!res) &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res = mysql_query (Connection,str.c_str ()); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;affected_count + + mysql_affected_rows (connection); &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (RES) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (Mysql_errno (connection)) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; "Error" &lt;&lt; Mysql_errnO (Connection) &lt;&lt; ":" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;&lt; mysql_error (connection) &lt;&lt; ' n ' &lt;&lt; Endl; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &NBSP;&NBSP} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getline (CIN,STR); &NBSP;&NBSP;&NBSP;&NBSP} &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; "Have affected" &lt;&lt; Affected_count & lt;&lt; "rows!" &lt;&lt; Endl; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;mysql_close (connection); &nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; "Connection to MySQL Server is closed ..." &lt;&lt; Endl; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return 0; } &nbsp; void Mysql_err_function (MySQL * connection) {&nbsp;&nbsp;&nbsp;&nbsp;if (Mysql_errno (connection)) &nbsp; &nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&Nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; "Error" &lt;&lt; Mysql_errno (Connection) &lt;&lt; ":" &nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;&lt; mysql_error (connection) &lt;&lt; Endl; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit (-1); &NBSP;&NBSP;&NBSP;&NBSP}}

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.