Click here to Skip to main content
Licence 
First Posted 8 Aug 2002
Views 107,042
Bookmarked 37 times

MFC and MySQL

By | 8 Aug 2002 | Article
MFC classes to fill ComoBoxes & ListBoxes with MySQL data.
 
Part of The SQL Zone sponsored by
See Also

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.

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

About the Author

Victor M. Valenzuela

Web Developer

Spain Spain

Member

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.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMySQL with TEXT type columns PinmemberAlexEvans12:53 6 Jul '07  
General2 dialogs Pinmembersongdongemdem11:36 7 Feb '07  
Generaldata migration from MS-SQL to MySQL PinmemberAlexEvans20:25 9 Oct '06  
Generalcode Pinmembergourav45619:46 5 Jun '06  
GeneralROW Pinmembergourav45619:23 5 Jun '06  
Questionmysql.dll - how to install it properly? Pinmembermimosa12:41 18 Mar '06  
Generalmysql_real_connect() PinmemberHervy4:13 9 Feb '06  
GeneralRe: mysql_real_connect() Pinmemberonlykeshu19:36 8 Mar '06  
Generalplz help me Pinmembersyed abbdul qavi8:36 26 Jan '06  
GeneralRe: plz help me Pinmembersath15423:22 13 Jul '06  
General"mysql_com.h" not work correct PinmemberLe Khac Chinh15:18 8 Feb '04  
GeneralRe: "mysql_com.h" not work correct PinsussAnonymous5:47 25 Jun '05  
QuestionYour example project ??? Pinmemberserup2:24 5 Dec '03  
GeneralDBF to MySQL Pinmembersantu1:55 5 Aug '03  
GeneralRe: DBF to MySQL Pinmemberfoolzz20:36 22 Apr '04  
GeneralWhy I rated your article low. PinmemberMichael A. Barnhart8:45 9 Aug '02  
Questionusing with WTL? PinmemberMyself8:20 9 Aug '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 9 Aug 2002
Article Copyright 2002 by Victor M. Valenzuela
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid