Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC
Article

MFC and MySQL

Rate me:
Please Sign up or sign in to vote.
2.87/5 (19 votes)
8 Aug 20021 min read 148.3K   7.2K   46   19
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:

SQL
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Spain Spain
Now, I works for Aurigae S.A a spanish services enterprise, specialized on information technologies for critical mission applications, mainly in the areas of payment system management, stock trading and telecommunications.

Comments and Discussions

 
Questionincludes/libs Pin
Member 1297261420-Feb-17 1:28
Member 1297261420-Feb-17 1:28 
GeneralMySQL with TEXT type columns Pin
AlexEvans6-Jul-07 12:53
AlexEvans6-Jul-07 12:53 
General2 dialogs Pin
songdongemdem7-Feb-07 11:36
songdongemdem7-Feb-07 11:36 
Generaldata migration from MS-SQL to MySQL Pin
AlexEvans9-Oct-06 20:25
AlexEvans9-Oct-06 20:25 
Generalcode Pin
yogendra kaushik5-Jun-06 19:46
yogendra kaushik5-Jun-06 19:46 
GeneralROW Pin
yogendra kaushik5-Jun-06 19:23
yogendra kaushik5-Jun-06 19:23 
Questionmysql.dll - how to install it properly? Pin
mimosa18-Mar-06 12:41
mimosa18-Mar-06 12:41 
Generalmysql_real_connect() Pin
Hervy9-Feb-06 4:13
Hervy9-Feb-06 4:13 
GeneralRe: mysql_real_connect() Pin
onlykeshu8-Mar-06 19:36
onlykeshu8-Mar-06 19:36 
Generalplz help me Pin
syed abbdul qavi26-Jan-06 8:36
syed abbdul qavi26-Jan-06 8:36 
GeneralRe: plz help me Pin
sath15413-Jul-06 23:22
sath15413-Jul-06 23:22 
General"mysql_com.h" not work correct Pin
Le Khac Chinh8-Feb-04 15:18
Le Khac Chinh8-Feb-04 15:18 
GeneralRe: "mysql_com.h" not work correct Pin
Anonymous25-Jun-05 5:47
Anonymous25-Jun-05 5:47 
QuestionYour example project ??? Pin
serup5-Dec-03 2:24
serup5-Dec-03 2:24 
GeneralDBF to MySQL Pin
santu5-Aug-03 1:55
santu5-Aug-03 1:55 
GeneralRe: DBF to MySQL Pin
foolzz22-Apr-04 20:36
foolzz22-Apr-04 20:36 
GeneralRe: DBF to MySQL Pin
siswoyo2-Nov-13 20:08
siswoyo2-Nov-13 20:08 
GeneralWhy I rated your article low. Pin
Michael A. Barnhart9-Aug-02 8:45
Michael A. Barnhart9-Aug-02 8:45 
Questionusing with WTL? Pin
Rene De La Garza9-Aug-02 8:20
Rene De La Garza9-Aug-02 8:20 
i want to use mysql++ with wtl but i get this error:
e:\mysql\include\null1.hh(26) : error C2872: 'ostream' : ambiguous symbol
d:\archivos de programa\microsoft visual studio\vc98\include\strstream(51) : error C2039: 'seekdir' : is not a member of 'ios'
d:\archivos de programa\microsoft visual studio\vc98\include\ios.h(106) : see declaration of 'ios'

i get 102 Errors
what can i do?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.