Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active July 30, 2022 18:21
Show Gist options
  • Save opsJson/18a76abe745eb75941e1675a80a1a4e8 to your computer and use it in GitHub Desktop.
Save opsJson/18a76abe745eb75941e1675a80a1a4e8 to your computer and use it in GitHub Desktop.
Easy MYSQL C API interface
#ifndef MYSQL_API_H
#define MYSQL_API_H
#include <mysql.h>
#include <stdio.h>
typedef struct MYSQL_CTX {
/* public */
unsigned int rows;
unsigned int columns;
/* private */
MYSQL *_conn;
MYSQL_RES *_res;
struct {
char *host;
char *user;
char *pass;
char *db;
int port;
} login;
} MYSQL_CTX;
MYSQL_CTX *MYSQL_begin(char *host, int port, char *user, char *pass, char *db);
MYSQL_ROW MYSQL_query(MYSQL_CTX *ctx, char *query);
MYSQL_ROW MYSQL_offset(MYSQL_CTX *ctx, unsigned int index);
void MYSQL_end(MYSQL_CTX *ctx);
static void MYSQL_error(MYSQL_CTX *ctx) {
MYSQL *conn;
char error_str[512];
unsigned int error_number = mysql_errno(ctx->_conn);
if (error_number == 0) return;
conn = ctx->_conn;
ctx->_conn = NULL;
if (error_number == 2003) return; //dont log cant connect
if (error_number == 1053) return; //dont log shutdown in progress
snprintf(error_str, sizeof(error_str)-1, "MYSQL ERROR %i: %s", error_number, mysql_error(conn));
MessageBox(NULL, error_str, "MYSQL ERROR!", MB_ICONERROR);
}
MYSQL_CTX *MYSQL_begin(char *host, int port, char *user, char *pass, char *db) {
MYSQL_CTX *ctx;
if ((ctx = malloc(sizeof(MYSQL_CTX))) == NULL) return NULL;
memset(ctx, 0, sizeof(MYSQL_CTX));
ctx->login.host = host;
ctx->login.port = port;
ctx->login.user = user;
ctx->login.pass = pass;
ctx->login.db = db;
return ctx;
}
void MYSQL_end(MYSQL_CTX *ctx) {
if (ctx == NULL) return;
if (ctx->_res) mysql_free_result(ctx->_res);
if (ctx->_conn) mysql_close(ctx->_conn);
memset(ctx, 0, sizeof(MYSQL_CTX));
free(ctx);
}
MYSQL_ROW MYSQL_query(MYSQL_CTX *ctx, char *query) {
MYSQL_ROW row;
if (ctx == NULL) return NULL;
while (ctx->_conn == NULL) {
if ((ctx->_conn = mysql_init(NULL)) == NULL) return NULL;
if (!mysql_real_connect(ctx->_conn, ctx->login.host,
ctx->login.user, ctx->login.pass,
ctx->login.db, ctx->login.port, NULL, 0))
MYSQL_error(ctx);
}
if (mysql_query(ctx->_conn, query)) {
MYSQL_error(ctx);
return NULL;
}
if (ctx->_res) {
mysql_free_result(ctx->_res);
ctx->rows = 0;
ctx->columns = 0;
}
if ((ctx->_res = mysql_store_result(ctx->_conn)) == NULL) {
MYSQL_error(ctx);
return NULL;
}
if ((row = mysql_fetch_row(ctx->_res)) == NULL) {
MYSQL_error(ctx);
return NULL;
}
ctx->rows = mysql_num_rows(ctx->_res);
ctx->columns = mysql_num_fields(ctx->_res);
return row;
}
MYSQL_ROW MYSQL_offset(MYSQL_CTX *ctx, unsigned int index) {
MYSQL_ROW row;
if (ctx == NULL) return NULL;
if (ctx->_res == NULL) return NULL;
if (index >= ctx->rows) return NULL;
mysql_data_seek(ctx->_res, index);
if ((row = mysql_fetch_row(ctx->_res)) == NULL) return NULL;
return row;
}
#endif /* MYSQL_API_H */
/*///////////////////////////////////
Testing:
///////////////////////////////////*/
int main(void) {
MYSQL_CTX *ctx;
MYSQL_ROW row;
int i;
ctx = MYSQL_begin("localhost", 3306, "root", "password", "database");
{
row = MYSQL_query(ctx, "SELECT * FROM Table;");
if (row == NULL) return 1;
for (i=0; i<ctx->columns; i++)
printf("%s\t", row[i]);
}
MYSQL_end(ctx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment