In this program, we will connect to the MyDb database, and then we will get the MySql version using the "SELECT" command and then print the MySql version on the console screen.
Program/Source Code:
The source code to get the MySql version using the SELECT statement in Linux is given below. The given program is compiled and executed successfully on Ubuntu 20.04.
//C program to get MySql version using
//SELECT statement in Linux.
#include <mysql.h>
#include <stdio.h>
#include <string.h>
int main()
{
char server[16] = "localhost";
char username[16] = "root";
char password[16] = "root";
char database[16] = "MyDb";
MYSQL* conn = mysql_init(NULL);
MYSQL_ROW record;
if (conn == NULL) {
printf("MySQL initialization failed");
return 1;
}
if (mysql_real_connect(conn, server, username, password, database, 0, NULL, 0) == NULL) {
printf("Unable to connect with MySQL server\n");
mysql_close(conn);
return 1;
}
if (mysql_query(conn, "SELECT VERSION()")) {
printf("Unable to connect with MySQL server\n");
mysql_close(conn);
return 1;
}
MYSQL_RES* rs = mysql_store_result(conn);
if (rs == NULL) {
printf("Unable to compile SQL statement\n");
mysql_close(conn);
return 1;
}
if (record = mysql_fetch_row(rs)) {
printf("MySql Version: %s\n", record[0]);
}
mysql_close(conn);
return 0;
}
In the above program, we included the mysql.h header file to use MySql connectivity related functions. Here, we created variables server, username, password, and database that are initialized values specific to the MySQL connectivity.
Here, we connected to the MyDb database in the MySql server using the mysql_real_connect() function and then we got the MySQL version using the SELECT statement and then print the version on the console screen.
In this program, we will connect to the MyDb database, and then we will get the MySql version using the "SELECT" command and then print the MySql version on the console screen.
Program/Source Code:
The source code to get the MySql version using the SELECT statement in Linux is given below. The given program is compiled and executed successfully on Ubuntu 20.04.
Output:
In the above program, we included the mysql.h header file to use MySql connectivity related functions. Here, we created variables server, username, password, and database that are initialized values specific to the MySQL connectivity.
Here, we connected to the MyDb database in the MySql server using the mysql_real_connect() function and then we got the MySQL version using the SELECT statement and then print the version on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer