Connect MFC to MySql database

Source: Internet
Author: User
Tags mysql command line

Connect MFC to MySql database:

1. First install mysql:

The version I use here is the mysql-essential-5.1.45-win32.msi. I encountered a small problem during installation. Here I will talk about it: the first installation is stuck when the last step starts the service, and the computer crashes. I forcibly quit, the result is not running properly, and then uninstall and reinstall it, because the password is set for the first time, even if the file is uninstalled, it still exists. Therefore, when the password is set for the second installation, the password to be set for the first time appears. Do not enter anything here, otherwise, an error occurs in the last step. I reinstalled it several times before I found the problem on baidu ).

Ii. configuration of VC6.0

1) Open the Options Option Under the Tools menu in the VC6.0 toolbar, and select "Includefiles" from the "Show Directories for:" drop-down list on the right of the directories tab ", then add the include directory path for local MySQL installation to the list box in the middle. My Files are D: \ Program Files \ MySQL Server 5.0 \ include ).

2) Select "Library files" in the "Show directories for:" drop-down list mentioned above, and then add the Lib directory path for local MySQL installation. The Lib directory contains two directories: debug and opt. We recommend that you select debug. My Files are D: \ Program Files \ MySQL Server 5.0 \ lib \ debug ).

3) Add "libmysql. lib" in "Project settings-> Link: Object/library modules ".

4) add # include "winsock. h "and # include" mysql. h "two files. Please note that the order of these two header files should not be reversed; otherwise, an error will be reported (this problem caused me to baidu for one morning ).

3. create databases and tables

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.

Mysql> show databases; // display all DATABASES. Be sure to press ";" and press Enter.

Mysql> create database mydb; // create database mydb

Mysql> USE mydb; // select the database mydb you created

Mysql> show tables; // display TABLES in the database

Mysql> create table mytable (username VARCHAR (50), password VARCHAR (200),); // CREATE a TABLE mytable: user name and password

Mysql> DESCRIBE mytable; // displays the table structure.

Iv. VC Programming

MYSQL mysql; // database connection handle

Mysql_init (& mysql );

If (! Mysql_real_connect (& mysql, "localhost", "root", "dxd", "mydb", 3306, NULL, 0 ))

{// Mydb indicates the database you have created. 3306 is the port number and can be set by yourself.

AfxMessageBox ("database connection failed ");

Return FALSE;

}

5. You can perform database operations through the above settings and connections.

We can connect to the MySql database through the MFC described above. I believe that you will be able to easily solve similar problems in your future work.

Related Article

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.