CPP (aka C ++) + MySQL

MySQL

Hello everyone, here I bring you an example of how a connection between C ++ and MySQL would be in GNU / Linux, of course this is just a basic example. First of all to compile I am going to use G ++ and they must have the packages installed libmysql ++ y libmysql ++ - dev . Well here is the example:

#include #include #include using namespace std; #define SERVER "HOST" #define USER "USR" #define PASSWORD "PSWD" #define DATABASE "example" int main () {MYSQL * connect; connect = mysql_init (NULL); if (! connect) {cout << "MySQL Initialization failed"; return 1; } connect = mysql_real_connect (connect, "HOST", "USER", "PASWD", "DATA BASE", 0, NULL, 0); if (connect) {cout << "connection Succeeded \ n"; } else {cout << "connection failed \ n"; } MYSQL_RES * res_set; MYSQL_ROW row; mysql_query (connect, "select * from data;"); unsigned int i = 0; res_set = mysql_store_result (connect); unsigned int numrows = mysql_num_rows (res_set); cout << endl; cout << "\ t -------------------------------------------- ------------------------- \ t "<< endl; while (((row = mysql_fetch_row (res_set))! = NULL)) {// cout << "% s \ n", row [i]! = NULL? row [i]: "NULL"; cout << "\ t | \ t" << row [i] << "\ t | \ t" << row [i + 1] << "\ t | \ t" << row [i + 2] << "\ t | \ t" << endl; cout << "\ t -------------------------------------------- ------------------------- \ t "<< endl; } mysql_close (connect); return 0; }

Well, if you don't know how to compile in G ++ this is an example:

g ++ -o main main.cpp -L / usr / include / mysql -lmysqlclient -I / usr / include / mysql

Then you run it and it would look like this:

------------------------------------- | 1 | Carmen | 46 | ------------------------------------- | 2 | Juan | 56 | --------------------------------------

Note: you can also compile it using MAKEFILE METHOD.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

33 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   pandev92 said

    C ++ ... that language defeated me a long time ago ...

    1.    eliotime3000 said

      I am left hungry for more with that programming language (the clarity and possibilities it offers are irreplaceable).

      1.    pandev92 said

        I'm not saying no, just because it was very difficult for me, python or mono, they seemed easier to use.

        1.    Giskard said

          Stick with Python. Makes you more productive in less time.

        2.    eliotime3000 said

          Python may be, to be able to correct bugs that slow down programs like the software-center or Ubiquiti, but Java, just to kill time.

          In fact, with C ++ I can understand the codes better (although most see a maze of unreadable codes) and I can have a clear notion about what it is going to do (although Java is easier to understand the code the first time , I see it as if it were BASIC and to tell the truth, I prefer to work with the OpenJDK instead of the Java JDK for fear that Oracle will put more bugs for the fuck).

    2.    nelson said

      me too

  2.   ferchmetal said

    Thank you friend.

  3.   Eduardo said

    Basic example? Then it will be the classic:
    class: 2 + 2 = 4
    task: 2 × 3 = 6
    Exam: if Eduardo has 20 oranges and he has to distribute them among 4 children, what is the body mass of the children?

    1.    eliotime3000 said

      1.- Solve the problem (the most important thing and what really takes longer for a novice).
      2.- Translate the solution to C ++ (if it is that language, the new ones will give up in most cases and more if they use GNU Emacs).
      3.- Debug as a precaution and compile (it is actually the other way around, but few do that process).

      PS: That test is anti-smug / pedantic.

  4.   Eduardo said

    The apologies of the case for the comment in triplicate, but the page gave me internal server error and perhaps because of this, when retrying, it was posted again.
    Greetings and thanks for the post!!

    1.    eliotime3000 said

      No problem. Anyway, I am commenting on Chromium nightly for Windows and I have not had any problems of that nature when commenting (although I recommend that you register so that the comment system will throw you more errors than Windows Millenium).

      1.    eliotime3000 said

        I wanted to say: "although I recommend that you register so that the comment system throws you fewer errors than Windows Millenium" [errare humanum est].

        1.    pandev92 said

          carpe dium LOL

  5.   eliotime3000 said

    That code is the most beautiful C ++ I've seen (KISS-style database viewer).

    Now, if this code can be compiled on Windows using GNU Emacs in conjunction with GCC on Windows (with MySQL / MariaDB included), it would be a milestone.

  6.   ratakill said

    Undoubtedly c ++ is the best that we have, I have done an unlimited number of tests about the performance and possibilities that it offers us and without a doubt nothing surpasses it of course that I recognize that it can be a headache for some

    1.    eliotime3000 said

      I admire that language, but I lack practice (and enough) to be able to master it completely and thus be able to put together a decent visual application with Qt.

  7.   jamin-samuel said

    Huy no

    I prefer Python

    😀

    1.    ratakill said

      Well every madman with his theme greetings

    2.    Giskard said

      Excellent choice.

    3.    eliotime3000 said

      [selfishness] Lazy [/ selfishness].

  8.   julian said

    I prefer VB.NET, it is much easier and has good support for SQL Server. In addition to being more productive than other languages ​​and one of the most requested when looking for work. I don't want to waste my time with C ++

    1.    pandev92 said

      If we get like this, the most requested is usually the crap of Java.

      1.    eliotime3000 said

        It is the same as VB.NET, but much more graphical and it is useless to work on GNU / Linux.

  9.   ratakill said

    I only recommend C ++ bc probe create a simple sell in c ++, python and qt and fence surprise:
    Python-> 7.6 mb
    Qt -> 27 mb (XD)
    C ++ -> 2.4 mb

    Well everyone who draws their own conclusions greetings

    1.    Giskard said

      Python 7.6MB ??? No way! Impossible. If they are nothing more than scripts. Or did you have images? Share the code. This is how we all see it and compare it. Convince us.

      1.    ratakill said

        You are right a small bug python did not consume 7.6 mb but if 6.2 mb so you tell me now ...
        This is the code and I think it cannot be more optimized:
        #! / usr / bin / python
        from gi.repository import Gtk

        win = Gtk.Window ()
        win.connect ("delete-event", Gtk.main_quit)
        win.show_all ()
        Gtk.main ()

        1.    Giskard said

          I imagine you mean RAM consumption. The thing is, in Python and in almost any language, you scale an asymptotic curve as you complicate the code. Just for testing I ran a game that I am developing with PyGame. Animation + background audio + sounds for events and barely reached 14MB. As obviously your program is not going to be as simple as a window but more complex, you will see in the end that, when you have enough code, large modifications in it will not bring you a greater increase in the use of RAM.

        2.    Giskard said

          To simplify what I am showing you a bit: You can't make a time profile if you don't run a segment of code a few thousand times, right? Well, in the same way you cannot tell if a language consumes a lot or a little just by looking at the simplest possible example. In fact, the trick is to look at the most complex you can.

          1.    ratakill said

            Well in my understanding at the programming level is 01010111, assembler and then c, so impossible that a high-level language like python surpasses ac / c ++ in performance (in every way) and well, what better than a very simple example like that of create a window to see a difference of 3.8 mb in only showing a simple window, when you add load to your program (a large program, that has a good weight with more than 5 forms, and apply classes, inheritance, polymorphism, virtual functions, pointers and more to give an example) I assure you that you will change your mind, I want to make it clear that I do not criticize other programming languages ​​and much less Python to the cantrario, I only comment and test with tests the advantages and disadvantages that I see with the naked eye without encouragement to deepen greetings

          2.    Giskard said

            Check this out:

            http://www.youtube.com/watch?v=cPVlYWxcu18

            But watch the full video. Do not cheat that if not you will simply give an opinion based on prejudice.

          3.    Giskard said

            In any case you did not understand me. As you complicate the matter, both will have more or less the same weight. On the Lazarus page they give a graph about that by the way, comparing Lazarus with C.
            In the end everything is relative friend. Because man does not live on RAM alone.

    2.    eliotime3000 said

      At least the QT is compatible with the Aqua, Win32 (and their Aero and Metro variants) interfaces and other interfaces except LXDE (it looks like Windows 95 to be honest).

      1.    pandev92 said

        gtk2 in aqua looks good ...