Rodolfo Bandeira

Rodolfo Bandeira

software, electronics, security, devops, thoughts

Using MySQL C API for Linux

Hi, if you are using Ubuntu linux just type this message below:

sudo apt-get install mysql-server
sudo apt-get install libmysql-dev

And after, create an file with extention “.c” and put this code into the file:

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

void main(void) {
  MYSQL connection;
  mysql_init(&connection);

  if (mysql_real_connect(&connection, "hostname", "user", "password", "database", 1, NULL, 0)) {
    printf("Yes! Connected\n");
    mysql_close(&connection);
  } else {
    printf("Fail!\n");
    printf("Error %d : %s\n", mysql_errno(&connection), mysql_error(&connection));
  }
}

When you compile it, you may put some variables. You have to use the command line below:

gcc -o test -L/usr/lib/mysql -lmysqlclient test.c

Have fun! :)

Last modified:


Share:
LinkedIn
WhatsApp