CS/DB

[MySQL] python과 C, C++로 MySQL 연동하기

O_oz 2024. 2. 23. 08:00
반응형

파이썬

sudo pip install mysql-connector-python
import mysql.connector

conn = mysql.connector.connect(
    host='서버IP',
    user='DB계정',
    password='DB계정비밀번호',
    database='DB이름'
)

cursor = conn.cursor()


sql_query = "INSERT INTO customer(id, ip) values (3, '10.10.10.10')"
cursor.execute(sql_query)

sql_query = "SELECT * FROM customer"
cursor.execute(sql_query)

result = cursor.fetchall()
print("INSERT")
for row in result:
    print(row)
print("\n\n")


sql_query = "UPDATE customer set ip = '10.10.10.11' WHERE id = 3"
cursor.execute(sql_query)

sql_query = "SELECT * FROM customer"
cursor.execute(sql_query)

result = cursor.fetchall()
print("UPDATE")
for row in result:
    print(row)
print("\n\n")

sql_query = "DELETE FROM customer WHERE id = 3"
cursor.execute(sql_query)

sql_query = "SELECT * FROM customer"
cursor.execute(sql_query)

result = cursor.fetchall()

print("DELTE")
for row in result:
    print(row)
cursor.close()
conn.close()

 

 

C

mysql.h가 있는지 확인

ls -al /usr/include/mysql/mysql.h

없다면 

sudo apt-get install libmysqlclient-dev

으로 다운로드 받자

 

#include "/usr/include/mysql/mysql.h"

#include <stdio.h>
#include <stdlib.h>

MYSQL *conn_ptr;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;

void db_init(void);

int main(void)
{

    db_init();

    if (mysql_query(conn_ptr, "INSERT INTO customer(id, ip) values (4, '10.10.10.12')"    ))
    {
        printf("query error\n");
        exit(1);
    }
    printf("INSERT\n");
    // 쿼리 적용
    if (mysql_query(conn_ptr, "SELECT * FROM customer"))
    {
        printf("query error\n");
        exit(1);
    }

    res_ptr = mysql_store_result(conn_ptr);

    while ((sqlrow = mysql_fetch_row(res_ptr)) != NULL)
    {
        printf("%s\t%s\n", sqlrow[0], sqlrow[1]);
    }
    printf("\n\n");

    if (mysql_query(conn_ptr, "UPDATE customer set ip ='10.10.10.100' WHERE id = 3"))
    {
        printf("query error\n");
        exit(1);
    }
    printf("UDPATE\n");
    if (mysql_query(conn_ptr, "SELECT * FROM customer"))
    {
        printf("query error\n");
        exit(1);
    }

    res_ptr = mysql_store_result(conn_ptr);

    while ((sqlrow = mysql_fetch_row(res_ptr)) != NULL)
    {
        printf("%s\t%s\n", sqlrow[0], sqlrow[1]);
    }
    printf("\n\n");

    if (mysql_query(conn_ptr, "DELETE FROM customer WHERE id = 3"))
    {
        printf("query error\n");
        exit(1);
    }
    printf("DELETE\n");

    if (mysql_query(conn_ptr, "SELECT * FROM customer"))
    {
        printf("query error\n");
        exit(1);
    }
 
     res_ptr = mysql_store_result(conn_ptr);

    while ((sqlrow = mysql_fetch_row(res_ptr)) != NULL)
    {
        printf("%s\t%s\n", sqlrow[0], sqlrow[1]);
    }
    printf("\n\n");

    mysql_close(conn_ptr);

    return 0;
}

void db_init(void)
{
    conn_ptr = mysql_init(NULL);

    if (!conn_ptr)
    {
        printf("init error\n");
        exit(1);
    }

    conn_ptr = mysql_real_connect(conn_ptr, "서버IP", "계정ID", "계정PW", "DB이름", 0, NULL, 0);

    if (conn_ptr)
    {
        printf("DB connect\n");
    }
    else
    {
        printf("DB connect error\n");
        exit(1);
    }
}

gcc 파일이름 -o 실행파일이름 -lmysqlclient

이렇게 컴파일하면 된다

 

C의 경우에는 중복으로 명령어가 입력되면 진행이 안되더라

아무래도 코드 형식을 다시 짜야할 것 같다

반응형