Now we are working on an interface through differentConnectionString to operate on different databases. To useMysql databaseYou have never used this database before. There are many access and SQL server instances. Through some information on the Internet and self-exploration, it is roughly clearC ++Connect to mysql. You can achieve this in two ways. I hope the content involved in this article will be helpful to you.
The first method is to connect using ADO,
The second method is to use mysql's own api functions for connection.
The first method can achieve my current needs by connecting different strings to connect different databases. Currently, only mysql, sqlserver, oracle, and access are connected. For access, because the SQL statement used to create a table is not compatible with standard SQL statements, some processing is required. The second method can only be used for mysql database connections. However, you do not need to install the MyODBC server program in this method.
No matter which method is used, you must first install the Mysql database. For the installation method, see "mysql installation and some precautions ". It is best to install a Navicat for mysql to facilitate operations on the mysql database. The two methods are described as follows:
1) connect to the MySql database through ADO
1. to connect to the MySql database through ADO, first install the MyODBC server program.The MyODBC version must match the MySql version. Otherwise, the database cannot be connected. The versions I use are mysql-5.1.48-win32.msi and mysql-connector-odbc-5.1.5-win32.msi.
After installation, choose Start> Settings> Control Panel> Administrative Tools> data source (ODBC)> User DSN> Add> select MySQL ODBC 5.1 Driver. For example:
Double-click MySQL ODBC 5.1 Driver. After configuration, click Test to perform the Test, for example,). If the connection is successful, the connection successful dialog box is displayed.
Data Source Name in: the content is the value corresponding to the DSN in the code.
For example, "DSN = MySqlODBC; server = localhost; database = test ".
2. After configuration, you can start encoding.
1) first import the ADO database. # Import "c: \ program files \ common files \ system \ ado \ msado15.dll" no_namespace rename ("EOF", "adoEOF "). Msado15.dll is not necessarily in this directory in your environment. modify it as needed. Or copy the msado15.dll file to your project directory and directly include # import "msado15.dll" \ no_namespace \ rename ("EOF", "adoEOF.
2) create a Connection object and connect to the database
(3) Execute SQL statements
_ Bstr_t bstrSQL is the input SQL statement. If TRUE is returned, the execution is successful. If FLASH is returned, an error message is returned.
For example, the following SQL statement used to create a testTable table:
3. Attach the mysql database operation for your reference if you have not installed Navicat for mysql..
Open "start-> All Programs-> MySQL Server 5.0-> MySQL Command Line Client.exe". If no password is set, press enter and a message is displayed, indicating that the Server is successfully started.
2) connect through MySQL's own API functions
1. To Connect using APIs, You need to load the mysql header file and lib file.Add \ MySQL Server 5.1 \ include to the additional include directory of VS2010. Find it in the directory where MySql is installed. Copy the libmysql. dll and libmysql. lib files to the created project directory. The header file contains the following content:
2. Encoding
1) connect to the mysql database
The data source pointer MYSQL m_sqlCon is defined in the header file;
// Connect to the MySql database
2) shut down the database
3) create a table
Attached MySQL API:
Through the introduction in this article, I hope you will understand the two methods for connecting C ++ to the mysql database, the two methods described above to connect C ++ to the mysql database are for your reference only. Here we just summarize these two methods for you, there may be more ways to connect C ++ to the mysql database. I hope I will continue to introduce it and hope it will help you.