Settings for connecting the MFC program to the MySQL database under vc6.0

Source: Internet
Author: User
Tags mysql command line

Abstract: This article describes in detail how to install and debug MySQL, and how to use VC to compile and implement functions such as adding, modifying, and deleting data.

1. Install MySQL

You can consider installing the mysql-5.0.41-win32 (can go to http://www.newhua.com/soft/3573.htm download), of course, you have a better version, pay attention to the choice of "fully installed" (only in this way will install the header files required for VC compilation ). After the installation, the server configuration will be performed. You can set your server login password or not.

Ii. configuration of vc6.0

(1) Open the options Option Under the Tools menu in the vc6.0 toolbar, and select "includefiles" in 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 options are D:/program files/MySQL Server 5.0/lib/debug ).

(3) Add "libmysql. lib" in "Project Settings-> link: Object/library modules ".

(4) Add the following content in stdafx. h:

# Include "mysql. H" # include "Winsock. H" # pragma comment (Lib, "libmysql. lib ")
(5) We recommend that you copy "libmysql. lib and libmysql. dll" to the directory of your project.

3. create databases and tables
Open "start-> All Programs-> mysql-> 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 a database

Mydbmysql> Use mydb; // select the database you created

Mydbmysql> show tables; // display tables in the database

Mysql> Create Table mytable (username varchar (100), visitelist varchar (200), remark varchar (200); // create a table mytable: User Name; access list;

Remarks

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

 

Iv. VC Programming

MySQL; // database connection handle

Mysql_init (& MySQL); If (! Mysql_real_connect (& MySQL, "localhost", "root", null, "mydb", 3306, null, 0 ))

{// Mydb indicates the port number of the database you created. You can set afxmessagebox ("database connection failed"); Return false ;}

(1) Implement the Add function
Cstring strusername, strlist, strremark, strsql; strsql. format ("insert into mytable (username, visitelist, remark) values (/'% S/',/'% S/',/'% S/')", strusername, strlist, strremark );

// Be sure to write it in one line, and you must have/'If (mysql_real_query (& MySQL, (char *) (lpctstr) strsql, (uint) strsql. getlength ())! = 0) {afxmessagebox ("failed to add ");}

(2) Implement the modification function
Cstring strusername, strlist, strremark, strsql, str_prename; // str_prename is used to record the row to be modified. For details, see the source code strsql. format ("Update mytable set username =/'% S/', visitelist =/'% S /', remark =/'% S/'where username =/' % S/'", strusername, strlist, strremark, str_prename); If (mysql_real_query (& MySQL, (char *) (lpctstr) strsql, (uint) strsql. getlength ())! = 0) {afxmessagebox ("failed to modify ");}

 

(3) Deletion
Cstring strsql; strsql. format ("delete from mytable where username =/'% S/'", str_prename); // you must have/'If (mysql_real_query (& MySQL, (char *) (lpctstr) strsql, (uint) strsql. getlength ())! = 0) {afxmessagebox ("failed to delete ");}

(4) read the table content to the clistctrl control m_list
M_list.deleteallitems (); char * ch_query; ch_query = "select * From mytable"; if (mysql_real_query (& MySQL, ch_query, (uint) strlen (ch_query ))! = 0) {afxmessagebox ("Table errors in Database");} cstring STR; mysql_res * result; mysql_row row; If (! (Result = mysql_use_result (& MySQL) {afxmessagebox ("failed to read the dataset");} int I = 0; while (ROW = mysql_fetch_row (result) {Str. format ("% s", row [0]); m_list.insertitem (I, STR); Str. format ("% s", row [1]); m_list.setitemtext (I, 1, STR); Str. format ("% s", row [2]); m_list.setitemtext (I, 2, STR); I ++;} mysql_free_result (result );

(5) shut down the database
Mysql_close (& MySQL); // it is best to write it to the ondestroy () function.

V. Conclusion

Based on the work of several netizens, this article describes how to install and debug MySQL, and how to use VC to compile and implement functions such as adding, modifying, and deleting data, I would like to provide some help to friends who are confused in MySQL's VC programming. I would like to express my gratitude to some netizens!

The symbol in values () should be/'% S/', which is a single quotation mark. For details, see the source code. (It may be an administrator error)

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.