Solved

Installing SQL++

Posted on 2004-11-02
269 Views
Last Modified: 2012-06-27
Hello

I'm now trying to connect to MySQL database using Visual C++ 6.0.

I've heard of installing SQL++ before such connection could be done. And I've downloaded the folder "mysql++-1.7.1-win32-vc++".

As I'm quite new to VC++, could anyone provide me some detailed steps showing what I should do afterwards?
0
Question by:kakaie
    8 Comments
     
    LVL 2

    Accepted Solution

    by:
    In order to connect to a mySQL database in C/C++, you will have to download and compile the proper library files. These files are called mySQL++, and can be found here. Download the zip file and unzip it to a temporary folder. Open the lib folder and copy the file mysql++.lib to your compilers Lib or Library directory.

    Create a new project in Visual C++ and press Ctrl+F7. Click the Link tab and type in mysql++.lib into the Library text-box.

    Copy all of the .h files from the download into your compiler's Include directory.


    Code


    Ok, time for the best part, the code. The first lines of code are ones that you will see echoed throughout all of the tutorials. The 2 lines of code include the proper files in order to be able to compile the project. They are:
    #include <stdio.h>
    #include <mysql.h>
    The several lines are used to #define the database details such as the host, the username, the password, and the database to connect to. Instead of creating 4 char pointers, I use 4 #define's like so:
    #define host "localhost"
    #define username "db_username"
    #define password "db_password"
    #define database "db"You will have to change the values in the quotes to match the values that fit you. The next line creates a pointer of type MYSQL to the active database connection:
    MYSQL *conn;
    This program, being as simple as it is, will contain only one user defined function, main() and the rest of the functions used will be from the mySQL++ API (application program interface).

    The next lines are ones that we all know and love:
    int main()
    {
    Followed by that, we tell mySQL that are are about to connect with the mysql_init() function. We pass a value of NULL to it because for our purposes, we don't need to go into it any further:
    conn = mysql_init(NULL);
    The next step is to actually connect to the database. This is where the conn variable and the host, username, password, and database #define's are used:
    mysql_real_connect(conn,host,username,password,database,0,NULL,0);
    The next 3 lines are code that define variables to get a result and row pointer from the database, along with a variable to increment in the loop if there is more than one row returned by the query:
    MYSQL_RES *res_set;
    MYSQL_ROW row;
    unsigned int i;

    Finally, we query the database with the following query using the mysql_query() function: "SELECT * FROM users" like so:
    mysql_query(conn,"SELECT * FROM users WHERE userid=1");
    After querying the result, we need to store the result data in the variable res_set we defined earlier. This is done like so:
    res_set = mysql_store_result(conn);
    In our example, there will most likely be more than one row returned (i.e., if there are more than one user in the users table of the database). If so, then you need to find how many rows are returned so that you can loop through each one and print the result of each one like so:
    unsigned int numrows = mysql_num_rows(res_set);
    Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks. This is done like so:
    while ((row = mysql_fetch_row(res_set)) != NULL)
    {
    However, looping through the number of rows returned is useless if you don't find the number of fields in each row (in this case, they are all the same because the query comes from the same table, but, if the query were produced on the fly [in the while loop], then the function mysql_num_fields() is used to find the number of fields). I hope that makes sense. Here goes:
    for (i=0; i<mysql_num_fields(res_set); i++)
    {
    And finally, after all this time, we print the stuff out using the plain old C standard function printf():
    printf("%s\n",row[i] != NULL ? row[i] : "NULL");
       }
    }
    Last but certainly not least, we close our connection to the database. When there are multiple connects and many users accessing the database at the same time, it is essential that you connect and disconnect as soon as possible, or you can have disasterous results. The final lines of code disconnect from the database and then exit the main() function:
    mysql_close(conn);
    return 0;
    }


    To compile the code, in Visual C++, you can press F7. If you have not already made a project, Visual C++ will ask you if you want to make one. Press Yes and continue on!

    As always, hope this helps.


    0
     
    LVL 2

    Expert Comment

    by:pb_india
    Also,
    you have to do is include windows.h before any of mysql includes.
    0
     

    Author Comment

    by:kakaie
    Thanks for your detailed descriptions!
    I'm trying it now and for the step:
    "Create a new project in Visual C++ and press Ctrl+F7. Click the Link tab and type in mysql++.lib into the Library text-box.", I could not follow since when I pressed Ctrl+F7, the default program would be compiled automatically and therefore, I couldn't see any selection for the Link tab.
    0
     

    Author Comment

    by:kakaie
    oh i think it is Alt+F7
    0
     

    Author Comment

    by:kakaie
    By the way, should I enter mysql++.lib into "Object/library modules" field, "Project Options" field or both?
    0
     
    LVL 2

    Expert Comment

    by:pb_india
    mysql++.lib should be entered :
    Project Settings->Link->INput->Object/Library Modules

    Also add the path of the lib under"Additional Library Path"


    Once you do Alt+F7 you can go to project Settings->Link tab
    0
     

    Author Comment

    by:kakaie
    1. There is a compilation warning:
    d:\abc.cpp(34) : warning C4244: 'initializing' : conversion from 'unsigned __int64' to 'unsigned int', possible loss of data

    line 34: unsigned int numrows = mysql_num_rows(res_set);

    Should I bother that?

    2. By the way, I don't see numrows being used in the program.

    3. I don't understand "add the path of the lib under"Additional Library Path"". How come I can't find "Additional Library Path" field in the Settings?

    4. Finally, when I try to build the whole project, the following error occurs:
    "Linking...
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/testingSQL.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe."

    The code compiled is shown below:

    #include <stdio.h>
    #include <windows.h>
    #include <mysql.h>

    #define host "localhost"
    #define username "root"
    #define password ""
    #define database "media"

    MYSQL *conn;
     
    int main()
    {  
    conn = mysql_init(NULL);
    mysql_real_connect(conn,host,username,password,database,0,NULL,0);

    MYSQL_RES *res_set;
    MYSQL_ROW row;
    unsigned int i;

    mysql_query(conn,"SELECT * FROM pf_group_gov WHERE serial_no=1");
    res_set = mysql_store_result(conn);

    unsigned int numrows = mysql_num_rows(res_set);

    while ((row = mysql_fetch_row(res_set)) != NULL)
    {
    for (i=0; i<mysql_num_fields(res_set); i++)
    {
    printf("%s\n",row[i] != NULL ? row[i] : "NULL");
       }
    }
    mysql_close(conn);
    return 0;
    }
    0
     

    Author Comment

    by:kakaie
    ok the linking error has been solved
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete C++ Projects

    Source the talented Expert Exchange community
    for top quality work on your C++ projects.

    Hire the best. Collaborate easily. Get quality work.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now