Access MySQL using C

With this tutorial I present to the community how to access a MySQL database from the C programming language and without using too many dependencies, with just

libmysqlclient

I hope it will be useful to those who needed this type of documentation and thus continue to contribute to the SL community and help those who do not have INTERNET.

Configuration

First we must check that we have the design libraries installed *-dev to be able to access from C / C ++ to MySQL.

mysql_config --libs

It should appear something like this:

-Wl, -Bsymbolic-functions –L / usr / lib / mysql -lmysqlclient rdynamic

In case you check that the library is not installed, we install it with the following command:

sudo apt-get install libmysqlclient-dev mysql_config --cflags -I / usr / include / mysql -DBIG_JOINS = 1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX

Since we have verified and installed what we need for the link between MySQL and C, we proceed to the creation of the database:

CREATE DATABASE test; USE test; CREATE TABLE data (id int AUTO_INCREMENT NOT NULL PRIMARY KEY, name varchar (40), age int); INSERT INTO data VALUES (NULL, 'Alma Hernandez', 28), (NULL, 'Jose Sanchez', 39), (NULL, 'Martin loera', 25), (NULL, 'Leonardo Cortez', 26), (NULL , 'Gustavo Romero', 25);

Programming and compilation

We continue to create the code:

Query.c

/ * libraries we will use * / #include / * library that allows us to make use of connections and queries with MySQL * / #include / * In order to use printf, etc. * / int main () {MYSQL * conn; / * connection variable for MySQL * / MYSQL_RES * res; / * variable that will contain the result of the query * / MYSQL_ROW row; / * variable that will contain the fields for each record consulted * / char * server = "localhost"; / * server address 127.0.0.1, localhost or ip address * / char * user = "root"; / * user to query the database * / char * password = "root"; / * password for the user in question * / char * database = "test"; / * name of the database to query * / conn = mysql_init (NULL); / * initialization to null the connection * / / * connect to the database * / if (! mysql_real_connect (conn, server, user, password, database, 0, NULL, 0)) {/ * define the connection parameters previously set * / fprintf (stderr, "% s \ n", mysql_error (conn)); / * if there is an error define which error was * / exit (1); } / * send SQL query * / if (mysql_query (conn, "select * from data")) {/ * definition of the query and the origin of the connection * / fprintf (stderr, "% s \ n", mysql_error ( conn)); exit (1); } res = mysql_use_result (conn);
	printf("ID \ tName \ t \ age \ n"); while ((row = mysql_fetch_row (res))! = NULL) / * loop through the variable res with all the records obtained for use * /
		printf("% s \ t% s \ t% s \ n", row [0], row [1], row [2]); / * the row variable becomes an array by the number of fields in the table * / / * the res variable is released and the connection is closed * / mysql_free_result (res); mysql_close (conn); }

We compile with:

gcc -o Query $ (mysql_config --cflags) Query.c $ (mysql_config -–libs)

CHECKOUT

We execute:

./Query

Source: Hugo4295's Blog


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.   koratsuki said

    Good that, saved in my arsenal of scripts and tutorials on console! +1 for you partner ...

  2.   rodrigo said

    Hello, many congratulations on everything that is on the web regarding this issue, yours was the closest to the solution. just a little doubt, why don't I get the executable ??

    at the time of comílar it does not mark any error but it does not generate the ./ser4

  3.   Andrélo said

    Any idea how to do it in Fedora?

  4.   victor of the o said

    I do not disagree with sharing the information but if the copyright is not placed since this example was made by me since 2011 and here is the address of the original source

    http://hugo4295.blogspot.mx/search?q=MYSQL

    1.    elav said

      No problem Victor, but if you notice I did not take the article from your site, but from another where unfortunately they did not put the source. However, right now I edit the article .. 😉

  5.   Alfonso Ovidio López Morales said

    excellent sharing knowledge great freedom

  6.   DANIELA FERNANDEZ said

    And how can insertions be made ???