Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ connect to and use MySQL database

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "how to connect and use MySQL database in C++". The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to connect and use MySQL database in C++".

1. C++ to connect MySQL database

First create a C++ project in VS, right-click the project name, and select Properties.

Select Platform Select

Select Configuration Manager

select new

Select X64 from the drop-down menu. determine

Select C/C++ -> General-> Attach include directory, add C:\Program Files\MySQL\MySQL Server 5.5\include (choose according to your installation directory)

Select Connector-> General-> Attach Library Catalog. Add C:\Program Files\MySQL\MySQL Server 5.5\lib;(select according to your installation directory)

Select Connector-> Input-> Attach Dependency. Add C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib;(select according to your installation directory)

Finally, copy the dynamic link library libmysql.dll to the X64 generation directory of the project. The DLL file is located in C:\Program Files\MySQL\MySQL Server 5.5\lib\directory

2. MySQL common API functions

mysql_affected_rows() Returns the number of rows affected by the most recent UPDATE, Delete, or INSERT query.

mysql_close() Closes a server connection.

mysql_connect() connects to a MySQL server. This function is not recommended; use mysql_real_connect() instead.

mysql_change_user() changes users and databases on an open connection.

mysql_create_db() creates a database. This function is not recommended; instead, use the SQL command CREATE DATABASE.

mysql_data_seek() searches for an arbitrary row in a query result set.

mysql_debug() Make a DBUG_PUSH with the given string.

mysql_drop_db() Discards a database. This function is not recommended; instead use the SQL command DROP DATABASE.

mysql_dump_debug_info() causes the server to write debug information to a log file.

mysql_eof() Determines whether the last line of a result set has been read. This function is deprecated; mysql_errno() or mysql_error() can be used instead.

mysql_errno() Returns the error number of the most recently invoked MySQL function.

mysql_error() returns an error message for the most recently invoked MySQL function.

mysql_escape_string() Special characters used to escape strings in SQL statements.

mysql_fetch_field() Returns the type of the next table field.

mysql_fetch_field_direct () Returns the type of a table field, given a field number.

mysql_fetch_fields() returns an array of all field structures.

mysql_fetch_lengths() Returns the length of all columns in the current row.

mysql_fetch_row() fetches the next row from the result set.

mysql_field_seek() places the column cursor on a specified column.

mysql_field_count() Returns the number of result columns for the most recent query.

mysql_field_tell() Returns the position of the field cursor for the last mysql_fetch_field().

mysql_free_result() frees memory used by a result collection.

mysql_get_client_info() returns client version information.

mysql_get_host_info() returns a string describing the connection.

mysql_get_proto_info() Returns the protocol version used for the connection.

mysql_get_server_info() Returns the server version number.

mysql_info() Returns information about the most recently executed query.

mysql_init() Gets or initializes a MYSQL structure.

mysql_insert_id() returns an ID generated by a previous query for an AUTO_INCREMENT column.

mysql_kill() Kills a given thread.

mysql_list_dbs() returns the database name that matches a simple regular expression.

mysql_list_fields() returns column names that match a simple regular expression.

mysql_list_processes() Returns a list of the current server thread.

mysql_list_tables() returns the table name that matches a simple regular expression.

mysql_num_fields() returns the number of columns that are heavy in a result set.

mysql_num_rows() returns the number of rows in a result set.

mysql_options() Sets connection options for mysql_connect().

mysql_ping() checks if the connection to the server is working and reconnects if necessary.

mysql_query() Executes a SQL query specified as a null-terminated string.

mysql_real_connect() connects to a MySQL server.

mysql_real_query() Executes SQL queries specified as counted strings.

mysql_reload() tells the server to reload the authorization table.

mysql_row_seek() searches for rows in the result set, using the value returned from mysql_row_tell().

mysql_row_tell() Returns the row cursor position.

mysql_select_db() connects to a database.

mysql_shutdown() shuts down the database server.

mysql_stat() returns server status as a string.

mysql_store_result() retrieves a complete result set for the client.

mysql_thread_id() Returns the ID of the current thread.

mysql_use_result() Initiates a search of the result set row by row.

3. C++ using MySQL database

Sample program, digest yourself, include API do not understand Google

#define _CRT_SECURE_NO_WARNINGS#include #includeusing namespace std;#pragma comment(lib,"libmysql.lib")#pragma comment (lib,"wsock32.lib")MYSQL *mysql = new MYSQL; //mysql concatenates MYSQL_FIELD *fd; //field array char field[32][32]; //A two-dimensional array of field names MYSQL_RES *res; //This structure represents a query result set of returned rows MYSQL_ROW column; //A type-safe representation of row data, representing columns of data rows char query[150]; //Query statements bool ConnectDatabase();bool QueryDatabase1();//bool QueryDatabase2();int main(){ ConnectDatabase(); QueryDatabase1(); //QueryDatabase2(); system("pause"); return 0;}bool ConnectDatabase(){ //Initialize mysql mysql_init(mysql); //Returns false if connection fails, returns true if connection succeeds if (! (mysql_real_connect(mysql, "localhost", "root", "123456", "company", 0, NULL, 0)) //host, user name, password, database name, port number (default 0 or 3306, etc.) in the middle, which can be written as parameters before passing them in { printf("Error connecting to database:%s\n", mysql_error(mysql)); return false; } else { printf("Connected...\ n"); return true; } return true;}bool QueryDatabase1(){ sprintf_s(query, "select * from t_dept"); //Execute query statement, here is query all, user is table name, without quotation marks, strcpy can also be used mysql_query(mysql, "set names gbk"); //Set the encoding format (SET NAMES GBK is also OK), otherwise Chinese garbled under cmd //Returns 0 query success, returns 1 query failure if (mysql_query(mysql, query)) //execute SQL statements { printf("Query failed (%s)\n", mysql_error(mysql)); return false; } else { printf("query success\n"); } //Get result set if (! (res = mysql_store_result(mysql))) //get the result set returned after sql statement ends { printf("Couldn't get result from %s\n", mysql_error(mysql)); return false; } //print data lines printf("number of dataline returned: %d\n", mysql_affected_rows(mysql)); //Get field info char *str_field[32]; //Defines a string array to store field information for (int i = 0; iname; } for (int i = 0; i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report