Created
February 16, 2019 14:00
-
-
Save joeke80215/b924da1b1b2aa5b840cef8cf7157f891 to your computer and use it in GitHub Desktop.
Mysql With C Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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