C++
  Home arrow C++ arrow Page 3 - Building a Store Application With MySQL++ ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Building a Store Application With MySQL++ and C/C++
By: Igal Raizman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 76
    2002-03-17

    Table of Contents:
  • Building a Store Application With MySQL++ and C/C++
  • Preparing the database
  • A basic program
  • Bringing it all together
  • Cases 2 and 3
  • Cases 4, 5 and 6
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Store Application With MySQL++ and C/C++ - A basic program


    (Page 3 of 7 )

    Before we begin this section, you will need to download MySQL++, which is the C++ library for interacting with MySQL databases. You can download it here and it's around 700KB. Just like MySQL, MySQL++ works on a verity of different platforms and compilers. Consult the documentation accompanying the download for the necessary information regarding your compiler. I personally use Borland's C++ Builder 5.0, but I've also worked with MySQL on Microsoft's Visual C++ 6.0, which is what I will be using throughout this article.

    Once you've downloaded MySQL++, it's extremely easy to include MySQL support into your applications. Extract the zip file to c:\mysql++. Copy c:\mysql++\mysql\lib\libmySQL.dll to c:\winnt\system32 and create a new console application in Visual C++. Click the Project -> Settings menu and go to the C/C++ tab. Choose the preprocessor category and add the following line to the additional include directories text box:

    c:\mysql++\include,c:\mysql++\mysql\include

    Click OK. Lastly, right click on the resource files node and add c:\mysql++\lib\mysql++.lib. If you're not using Visual C++ then consult the documentation that comes with your version of MySQL++. With that said, we can begin writing a very basic program to interact with our database. Our program will connect to and then disconnect from the database.

    Firstly, we need to include some header files:

    #include <stdio.h>

    #include <mysql++.h>


    Next we will create a pointer to the MySQL structure that will hold our connection. This structure is defined in the MySQL header file. What it basically does is represent a handle to a database connection. If you would like to connect to more than one database in your application then you will need to declare more than one MYSQL structure.

    MYSQL *pConnection;

    To initialize our MYSQL structure we must call mysql_init(). It will allocate the required memory, initialize and then return a pointer to a MYSQL object:

    pConnection = mysql_init(NULL);

    To be on the safe side, you might want to check that a pointer to a MYSQL object is actually returned. You can do this by checking if pConnection is null or not. Next, we actually connect to the database using mysql_real_connect(). Here's the declaration of mysql_real_connect():

    mysql_real_connect MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned int client_flag)

    As you can see, there's quite a lot of parameters that the mysql_read_connect function accepts. Here's a brief breakdown of what each parameters represent:
    • mysql: The first parameter is the MYSQL structure we've created above. It will hold the information concerning the connection once we connect to MySQL.
    • host: The second parameter is the location of the database server. If the database is installed on your computer locally, then you would use "localhost".
    • user/passwd: The third and fourth parameters are the username and password of a valid MySQL user account.
    • db: The fifth parameter is the database you would like to connect to. In our case this will be "Article1".
    • port: The sixth parameter is the port your database server will listen on. Pass 0 to indicate that the default MySQL port should be used.
    • unix_socket: The seventh parameter is the Unix socket number. We will pass NULL to indicate that we don't require a special Unix socket.
    • client_flag: The last parameter is reserved for certain flags we may want to use, for example passing CLIENT_SSL will use the SSL encryption protocol or CLIENT_COMPRESS will use a compression protocol. For a full list of flags and their descriptions, consult the MySQL++ documentation. We will pass a 0, as we do not require any of the flags.
    Here's how our function call will look (note that the function will return a NULL value on an unsuccessful connection attempt):

    mysql_real_connect(pConnection, "localhost", "admin", "password", "Article1", 0, NULL, 0);z

    Before we take our application to the next level, you have to know one last thing which is how to disconnect from the database. This is accomplished by a simple call to mysql_close(). The functionrequires a pointer to a MYSQL object as its only parameter. Mysql_close() will close the connection and free the memory allocated to the MYSQL structure:

    mysql_close(pConnection);

    Putting everything together, here's our first C++ application that uses MySQL++ to connect to and disconnect from our MySQL database, Article1:

    #include <stdio.h>

    #include <mysql++.h>

    MYSQL *pConnection;

    int main(int argc, char* argv[])

    {

    pConnection = mysql_init(NULL);

    if(!pConnection) //error, quit the program

    return 0;



    printf("Attempting to connect\n");



    if(mysql_real_connect(pConnection,"localhost","admin","password","Article1",0,NULL,0) == NULL) //error, quit the program

    return 0;



    printf("Connected to the database\n");

    mysql_close(pConnection);

    printf("Connection Terminated\n");



    return 0;

    }

    More C++ Articles
    More By Igal Raizman


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT