'분류 전체보기'에 해당되는 글 514건

  1. 2018.11.20 MySQL C API
  2. 2018.11.19 PyMySQL 2

MySQL C API

C, C++ 2018. 11. 20. 11:31 |
반응형

MySQL C API 는 C 프로그램이 MySQL 데이터 베이스 내용에 접근 할 수 있도록 MySQL 서버/클라이언트 프로토콜의 저수준 접근을 제공 한다.

 

5.7 버전 참고 문서: MySQL C API

 

설정:

  • 버전 설정: X64

  • Project - XXX Properties... - Configuration Properties - C/C++ - General - Additional Include Directories - C:\Program Files\MySQL\MySQL Server 5.7\include 추가
  • Project - XXX Properties... - Configuration Properties - Linker - General - Additional Library Directories - C:\Program Files\MySQL\MySQL Server 5.7\lib 추가

 

 

  • Project - XXX Properties... - Configuration Properties - Linker - Input - Additional Dependencies - libmysql.lib 추가 하거나 소스에 아래 코드 추가
    #pragma comment (lib, "libmySQL.lib")
  • 실행파일 폴더에 libmysql.dll 복사

 

예제:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <my_global.h>
#include <mysql.h>
 
#pragma comment (lib, "libmySQL.lib")
 
int main()
{
    MYSQL mysql;            // MySQL 정보를 담을 구조체
    MYSQL* mysqlPtr = NULL;    // MySQL connection 핸들러
    MYSQL_RES* Result = NULL;        // 쿼리 성공시 결과를 담는 구조체 포인터
    MYSQL_ROW Row;            // 쿼리 성공시 결과로 나온 행의 정보를 담는 구조체
    int stat;                // 쿼리 요청 후 결과
 
    printf("MySQL Client Version: %s\n\n", mysql_get_client_info());
    // Returns client version information as a string
 
    mysql_init(&mysql); // Gets or initializes a MYSQL structure 
 
    mysqlPtr = mysql_real_connect(&mysql, "127.0.0.1""root""1234""shopdb"3306, (char*)NULL0);
    // Connects to a MySQL server
 
    if (mysqlPtr == NULL)
    {
        printf("MySQL connection error: %s\n", mysql_error(&mysql));
        // Returns the error message for the most recently invoked MySQL function
        return 1;
    }
 
    // MySQL에서 사용하는 문자세트를 Visual Studio가 사용하는 euc-kr로 바꾸기
    mysql_query(mysqlPtr, "set session character_set_connection=euckr");
    mysql_query(mysqlPtr, "set session character_set_results=euckr");
    mysql_query(mysqlPtr, "set session character_set_client=euckr");
 
    const char* Query = "SELECT * FROM memberTBL";
    stat = mysql_query(mysqlPtr, Query);    // Executes an SQLquery specified as a null-terminated string
    if (stat != 0)
    {
        printf("MySQL connection error: %s\n", mysql_error(&mysql));
        return 1;
    }
 
    Result = mysql_store_result(mysqlPtr);    // Retrieves a complete result set to the client
    while ((Row = mysql_fetch_row(Result)) != NULL)    // Fetches the next row from the result set 
    {
        printf("%s %s %s\n", Row[0], Row[1], Row[2]);
    }
 
    mysql_free_result(Result);    // Frees memory used by a result set
    mysql_close(mysqlPtr);    // Closes a server connection
 
    return 0;
}
cs

5.7 버전의 MySQL은 빌드하면 C2011('struct' type redefinition) 에러가 발생하는 경우가 있다.

C2011 에러가 발생 하면 my_global.h 파일에 #define HAVE_STRUCT_TIMESPEC 코드를 추가해 준다.

 

 

반응형
Posted by J-sean
:

PyMySQL

Python 2018. 11. 19. 21:54 |
반응형

PyMySQL은 Python에서 MySQL사용을 도와주는 순수 Python MySQL client library이다.

 

설치 조건:

  • Python -- one of the following:
  • MySQL Server -- one of the following:
  • 설치:

    Package is uploaded on PyPI.

    You can install it with pip:

    $ python3 -m pip install PyMySQL

    관련 문서:

    Documentation is available online: https://pymysql.readthedocs.io/

    For support, please refer to the StackOverflow.



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import pymysql
     
    connection = pymysql.connect(host = '127.0.0.1', port = 3306, user = 'root', password = '1234', db = 'shopdb', charset = 'utf8')
    # Representation of a socket with a mysql server. The proper way to get an instance of this class is to call connect().
    # Establish a connection to the MySQL database.
     
    cursor = connection.cursor() # Create a new cursor to execute queries with
     
    sql = "select * from membertbl"
    cursor.execute(sql) # Execute a query
     
    rows = cursor.fetchall() # Fetch all the rows
    for member in rows:
        print(member)
     
    connection.close() # Closing a cursor just exhausts all remaining data.
    cs


    반응형
    Posted by J-sean
    :