65.9K
CodeProject is changing. Read more.
Home

MFC and MySQL

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.87/5 (19 votes)

Aug 9, 2002

1 min read

viewsIcon

150267

downloadIcon

7215

MFC classes to fill ComoBoxes & ListBoxes with MySQL data.

Sample Image - DlgMySql.jpg

Introduction

This article explains how to get connected to MySQL database and retrieve data using the MySQL C++ API.

The sample uses the classes CMySqlLstBox, CMySqlCboBox: very simple C++ classes derived from the standard MFC classes CListBox and CComboBox with the addition of minimal functionality to permit connection to a MySQL database.

MySQL C++ API

To obtain the MySQL C++ API, please visit the MySql web site for detailed information.

Target

Develop Windows clients to the MySQL database in MFC using the MySQL C++ API.

Includes

#include "iostream"
#include "iomanip"
#include "mysql++" // Part of the MySQL C++ API

See the include file: usr_mysql.h.

Used Libraries

  • libmySQL.lib
  • mysql++.lib

Used Dll

libmySQL.dll

Simple Database

The sample access is a small table called paises which contains a list of countries.

This is the sample SQL script to generate the table:

DROP TABLE IF EXISTS paises;
CREATE TABLE paises (
CodPais tinyint(4) NOT NULL auto_increment,
Nombre varchar(40) default NULL,
Prefijo tinyint(4) default NULL,
Code char(2) NOT NULL default '',
PRIMARY KEY (CodPais)
) TYPE=MyISAM;

Use

  1. Include the files:
    #include "MySqlCboBox.h" // For Using Combo Boxes
    #include "MySqlLstBox.h" // For Using List Boxes
  2. Use Class Wizard to add member variables to your Dialog.
  3. Change in the AFX_DATA section:
    • Replace CListBox to CMySqlLstBox
    • Replace CComboBox to CMySqlCboBox
  4. In the OnInitDialog, call the CMySqlLstBox or/and CMySqlCboBox member:
    FillData(Field Name in the table from get the data,
    Field Name in the table from get the code,
    TableName,
    Where contition for the query,if needed)

    To fill the controls with the database records.

Remarks

Remember to change your Project Settings to point to the MySQL include files and the MySQL libs.