Incorporating MySQL operations into C involves first installing the MySQL C API library and then utilizing functions from the mysql.h header file for tasks like connection and querying. Below is a simple example:
#include <stdio.h>
#include <mysql/mysql.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "your_username";
char *password = "your_password";
char *database = "your_database";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "SELECT * FROM your_table")) {
fprintf(stderr, "%s", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL) {
printf("%s", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}
1. Installing the MySQL C API Library
Before starting to work with MySQL in C, it's necessary to install the MySQL C API library, which can be done using the following command:
sudo aptget install libmysqlclientdev
2. Including Header Files
In a C program, include the following header files:
#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h>
3. Establishing Database Connection
Use the mysql_init()
and mysql_real_connect()
functions to establish a database connection:
MYSQL *conn; conn = mysql_init(NULL); if (!mysql_real_connect(conn, "localhost", "username", "password", "database", 0, NULL, 0)) { fprintf(stderr, "%s", mysql_error(conn)); exit(1); }
Use the mysql_query()
function to execute SQL queries:
if (mysql_query(conn, "SELECT * FROM table_name")) { fprintf(stderr, "%s", mysql_error(conn)); exit(1); }
Utilize the mysql_store_result()
and mysql_fetch_row()
functions to handle query results:
MYSQL_RES *result = mysql_store_result(conn); if (result == NULL) { fprintf(stderr, "%s", mysql_error(conn)); exit(1); } int num_fields = mysql_num_fields(result); MYSQL_ROW row; while ((row = mysql_fetch_row(result))) { for (int i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf(""); }
Free up resources using the mysql_free_result()
and mysql_close()
functions:
mysql_free_result(result); mysql_close(conn);
Here's a complete example demonstrating how to connect to a MySQL database and execute a query using C:
#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> int main() { MYSQL *conn; MYSQL_RES *result; MYSQL_ROW row; int num_fields; conn = mysql_init(NULL); if (!mysql_real_connect(conn, "localhost", "username", "password", "database", 0, NULL, 0)) { fprintf(stderr, "%s", mysql_error(conn)); exit(1); } if (mysql_query(conn, "SELECT * FROM table_name")) { fprintf(stderr, "%s", mysql_error(conn)); exit(1); } result = mysql_store_result(conn); if (result == NULL) { fprintf(stderr, "%s", mysql_error(conn)); exit(1); } num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { for (int i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf(""); } mysql_free_result(result); mysql_close(conn); return 0; }
By following these steps, you can operate MySQL in C and execute queries successfully.
```Don't forget to replace 'your_username', 'your_password', 'your_database', and 'your_table' with your actual credentials and table name.
Feel free to ask if you have any questions or need further assistance.
Thank you for reading!
Remember to comment, like, and share this article if you found it helpful.