5

I am using codeblocks in a windows environment.This is my c code to connect to mysql database.

/* Simple C program that connects to MySQL Database server*/
    #include <mysql.h>
    #include <stdio.h>

    main() {
      MYSQL *conn;
      MYSQL_RES *res;
      MYSQL_ROW row;

      char *server = "localhost";
      char *user = "root";
      //set the password for mysql server here
      char *password = "*********"; /* set me first */
      char *database = "Real_flights";

      conn = mysql_init(NULL);

      /* Connect to database */
      if (!mysql_real_connect(conn, server,
            user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
      }

      /* send SQL query */
      if (mysql_query(conn, "show tables")) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
      }

      res = mysql_use_result(conn);

      /* output table name */
      printf("MySQL Tables in mysql database:\n");
      while ((row = mysql_fetch_row(res)) != NULL)
          printf("%s \n", row[0]);

      /* close connection */
      mysql_free_result(res);
      mysql_close(conn);
    }

I hav an error popping up saying

mysql.h : No such file or directory

When is use c++ in codeblocks, including mysql.h has no issues.But when i do it using c, the above error pops up.

alk
  • 69,737
  • 10
  • 105
  • 255
Mr.Shark
  • 128
  • 1
  • 1
  • 10
  • 2
    Is the MySQL library and its development files (header files and linker libraries) installed in a default location where the compiler and linker looks? If not (which it seems it isn't) then you need to configure your project so that it can find those files. You probably have made that configuration for your C++ project but just forgot about it, so I suggest you check the (preporcessor and linker libraries) settings of the C++ project. – Some programmer dude Apr 25 '19 at 05:23
  • How do i check it in codeblocks? – Mr.Shark Apr 25 '19 at 05:29
  • My bet goes on `#include ` – alk Apr 25 '19 at 06:02
  • Related https://stackoverflow.com/q/14604228/694576 if not a dupe to. – alk Apr 25 '19 at 06:03
  • OT: regardless of what some compilers allow, The return type from `main()` is `int`, not a blank – user3629249 Apr 25 '19 at 19:02

2 Answers2

4

To use the mysql on a windows machine one has to download the mysql connector for windows. After installing it an leaving defaults, the header and lib files can be found at.

C:\Program Files\MySQL\Connector C++ 8.0

C:\Program Files\MySQL\MySQL Server 8.0

Usually you have to detect the right header and library for you and to link them properly. Please refer to this tutorial

Community
  • 1
  • 1
vasile_t
  • 372
  • 2
  • 12
  • 1
    If an answer works for you, you should indicate that by accepting it (click on the check mark beside the answer). That will award the answerer and infirm others that the question had been dealt with. – rici Apr 25 '19 at 16:54
0

Solution for mysql

Install mysql server, then go to the include directory for mysql server, then copy folder mysql and header files into folder, example

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include

then comment in setup_windows.py into mysqlclent directory line client = 'mariadbclient' and uncomment previous line, then copy the file mysqlclient.lib which stored in mysql server lib directory into

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.16299.0\ucrt\x64.

this solution worked for me.