Skip to content

Instantly share code, notes, and snippets.

@joeke80215
Created February 16, 2019 14:00
Show Gist options
  • Save joeke80215/b924da1b1b2aa5b840cef8cf7157f891 to your computer and use it in GitHub Desktop.
Save joeke80215/b924da1b1b2aa5b840cef8cf7157f891 to your computer and use it in GitHub Desktop.
Mysql With C Demo
#include <stdio.h>
#include <my_global.h>
#include <mysql.h>
int main(void)
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "127.0.0.1", "[mysql user]",
"[mysql password]", "[mysql database]", 3306, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "CREATE TABLE COMPUTER_LANGUAGE(language VARCHAR(10))"))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO COMPUTER_LANGUAGE VALUES('Golang')"))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO COMPUTER_LANGUAGE VALUES('C')"))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO COMPUTER_LANGUAGE VALUES('Python')"))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO COMPUTER_LANGUAGE VALUES('Kotlin')"))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO COMPUTER_LANGUAGE VALUES('PHP')"))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment