Create a gist now

Instantly share code, notes, and snippets.

@kakkun61 /Dockerfile
Last active Feb 27, 2017

What would you like to do?
FROM ubuntu
RUN apt update
RUN apt install -y libmysqlclient-dev gcc rsync
#include<mysql.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
MYSQL *mysql = mysql_init(NULL);
printf("%p\n", mysql);
{
const char *host = getenv("TWITTER_REVIEW_DB_PORT_3306_TCP_ADDR");
const char *password = getenv("TWITTER_REVIEW_DB_ENV_MYSQL_PASSWORD");
const char *database = getenv("TWITTER_REVIEW_DB_ENV_MYSQL_DATABASE");
const char *user = getenv("TWITTER_REVIEW_DB_ENV_MYSQL_USER");
const int port = atoi(getenv("TWITTER_REVIEW_DB_PORT_3306_TCP_PORT"));
MYSQL *error = mysql_real_connect(mysql, host, user, password, database, port, "", 0);
if (error == NULL) {
fprintf(stderr, "mysql_real_connect: error\n");
mysql_close(mysql);
exit(1);
}
}
printf("server info: %s\n", mysql_get_server_info(mysql));
{
my_bool error = mysql_autocommit(mysql, 0);
if (error) {
fprintf(stderr, "mysql_autocommit: error\n");
mysql_close(mysql);
exit(1);
}
}
{
int error = mysql_query(mysql, "START TRANSACTION");
if (error) {
fprintf(stderr, "mysql_query: error\n");
mysql_close(mysql);
exit(1);
}
}
{
MYSQL_STMT *stmt = mysql_stmt_init(mysql);
if (stmt == NULL) {
fprintf(stderr, "mysql_stmt_init: error\n");
mysql_close(mysql);
exit(1);
}
{
const char * const stmt_str = "SELECT `display_name` FROM `user` WHERE `id` = ?";
int error = mysql_stmt_prepare(stmt, stmt_str, strlen(stmt_str));
if (error) {
fprintf(stderr, "mysql_stmt_prepare: error\n");
mysql_close(mysql);
exit(1);
}
}
MYSQL_RES *result_metadata = mysql_stmt_result_metadata(stmt);
if (result_metadata == NULL) {
fprintf(stderr, "mysql_stmt_result_metadata: error\n");
mysql_close(mysql);
exit(1);
}
MYSQL_FIELD *field;
while(field = mysql_fetch_field(result_metadata))
{
printf("field name: %s\n", field->name);
}
long long user_id = 1;
MYSQL_BIND param;
unsigned long user_id_length = sizeof(user_id);
memset(&param, 0, 1);
param.buffer_type = MYSQL_TYPE_LONGLONG;
param.buffer = &user_id;
param.buffer_length = sizeof(user_id);
param.is_null = NULL;
param.length = &user_id_length;
{
my_bool error = mysql_stmt_bind_param(stmt, &param);
if (error) {
fprintf(stderr, "mysql_stmt_bind_param: error\n");
mysql_close(mysql);
exit(1);
}
}
{
int error = mysql_stmt_execute(stmt);
if (error) {
fprintf(stderr, "mysql_stmt_execute: error\n");
mysql_close(mysql);
exit(1);
}
}
MYSQL_BIND result;
char display_name[100];
unsigned long display_name_length;
my_bool display_name_error;
memset(&result, 0, 1);
result.buffer = display_name;
result.buffer_length = sizeof(display_name) * sizeof(display_name[0]);
result.is_null = NULL;
result.length = &display_name_length;
result.error = &display_name_error;
{
my_bool error = mysql_stmt_bind_result(stmt, &result);
if (error) {
fprintf(stderr, "mysql_stmt_bind_result\n");
mysql_close(mysql);
exit(1);
}
}
// {
// int fetch_result = mysql_stmt_fetch(stmt);
// switch (fetch_result) {
// case MYSQL_NO_DATA:
// printf("mysql_stmt_fetch: MYSQL_NO_DATA\n");
// break;
// case MYSQL_DATA_TRUNCATED:
// printf("mysql_stmt_fetch: MYSQL_DATA_TRUNCATED\n");
// break;
// default:
// printf("mysql_stmt_fetch: %d\n", fetch_result);
// }
// printf("display_name: %s\n", display_name);
// switch (result.buffer_type) {
// case MYSQL_TYPE_BLOB:
// printf("display_name type: MYSQL_TYPE_BLOB\n");
// default:
// printf("display_name type: %d\n", result.buffer_type);
// }
// printf("display_name_length: %ld\n", display_name_length);
// printf("display_name_error: %d\n", display_name_error);
// }
{
my_ulonglong rows = mysql_stmt_affected_rows(stmt);
printf("mysql_stmt_affected_rows: %ld\n", (long)rows);
}
{
MYSQL_STMT *stmt2 = mysql_stmt_init(mysql);
if (stmt == NULL) {
fprintf(stderr, "mysql_stmt_init: error\n");
mysql_close(mysql);
exit(1);
}
{
const char * const stmt_str = "SELECT `email` FROM `user` WHERE `id` = ?";
int error = mysql_stmt_prepare(stmt2, stmt_str, strlen(stmt_str));
if (error) {
fprintf(stderr, "mysql_stmt_prepare: error\n");
fprintf(stderr, "error message: %s\n", mysql_stmt_error(stmt2));
mysql_close(mysql);
exit(1);
}
}
}
mysql_free_result(result_metadata);
{
my_bool error = mysql_stmt_close(stmt);
if (error) {
fprintf(stderr, "mysql_stmt_close: error\n");
mysql_close(mysql);
exit(1);
}
}
}
mysql_close(mysql);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment