Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use API of MySql to connect to MySql database

Shulou Source: shulou.com Published: 2022-05-31 18:45:37 09月19日 Update

This article introduces the knowledge about "how to use MySql API to connect MySql database". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

C++ connects to MySQL databases in two ways:

C API via MySQL (decided to use this way)

You can also use MySQL++.

[Mysql++ is an officially released API in C++ designed for MySQL. Mysql++ is a re-encapsulation of Mysql's C-Api, developed and written in STL(Standard Template Language), and provides C++ developers with a set of mechanisms for manipulating databases as conveniently as operating STL containers]

2. Connector c++ via MySQL

MySQL C++ Driver implementation is based on the JDBC specification.

MySQL Connector/C++ is a MySQL connector developed by Sun Microsystems. It provides OO based programming interfaces and database drivers to operate MySQL servers. Unlike many other existing C++ interface implementations, Connector/C++ follows the JDBC specification. In other words, Connector/C++ Driver API is mainly based on Java language JDBC interface. JDBC is the Java language's standard industry interface to various databases. Connector/C++ implements most of the JDBC specification.

Learn how to use MySql API to connect to MySql database

I. Engineering settings

Point the header file path of the project to the location of mysql.h, the same file in Mysql installation directory, and point the connection library path to the location of libmysql.lib:

Specific configuration steps:

1. C/C++-> General-> Additional include directory on the project property page points to: C:\Program Files\MySQL\MySQL Server 5.1\include

2. Linker of project property page-> General-> Additional library directory points to:C:\Program Files\MySQL\MySQL Server 5.1\lib\opt.

Add libmysql.lib to linker-> input-> additional dependencies (you can also import libmysql.lib by adding #pragma comment(lib,"D:\Program Files\MySQL\MySQL Server 5.6\lib\libmysql. lib") at the beginning of the program code)

4. Copy libmysql.dll under D:\Program Files\MySQL\MySQL Server 5.6\lib to the debug folder of the project, that is, under a folder with the output file.

API for connecting MySQL and retrieving data from MySQL

Simple connection database, get database information:

#include

< windows.h>

#include

< mysql.h>

#include

< string>

#include

< iostream>

using namespace std;int main(){ const char user[] = "root"; const char pswd[] = "root"; const char host[] = "localhost"; const char table[] = "bookdb";//database name unsigned int port = 3306; MYSQL myCont; MYSQL_RES *result; MYSQL_ROW sql_row; int res; mysql_init(&myCont); if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0)) { mysql_query(&myCont, "SET NAMES GBK"); //Set encoding format res = mysql_query(&myCont, "select * from bookInfo");//query if (! res) { result = mysql_store_result(&myCont); if (result) { while (sql_row = mysql_fetch_row(result))//get specific data { cout

Tags: Data databases services servers results queries characters strings files functions fields interfaces clocking one line information point location cursor content quantity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft OPPO Reno Redmi Shulou Information