Skip to content

Instantly share code, notes, and snippets.

@halloweeks
Created November 15, 2022 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halloweeks/08d1e69de3fb11a204f0689aa0c09a9d to your computer and use it in GitHub Desktop.
Save halloweeks/08d1e69de3fb11a204f0689aa0c09a9d to your computer and use it in GitHub Desktop.
MySQL connection with c language
#include <stdio.h>
#include <stdlib.h>
#include <mariadb/mysql.h>
int main() {
MYSQL *connection;
MYSQL_RES *results;
MYSQL_ROW row;
connection = mysql_init(NULL);
if (!mysql_real_connect(connection, "127.0.0.1", "root", "1234", "test", 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(connection));
exit(EXIT_FAILURE);
}
if (mysql_query(connection, "show tables")) {
//fprintf(stderr, "%s\n", mysql_error(connection));
exit(EXIT_FAILURE);
}
results = mysql_use_result(connection);
// int num = mysql_num_rows(results);
// output table name
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(results)) != NULL) {
printf("%s \n", row[0]);
}
// close connection
mysql_free_result(results);
mysql_close(connection);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment