저도 지금 학교 프로젝트 나온 것 때문에 C API가 어떻게 되어 있는지
보고 있는데요.. PHP로 코딩해 본 적이 있어서 그런지 그렇게까지 어
렵지는 않은 것 같더군요. (PHP보다는 신경쓸 부분이 많지만요. ^^;;)
일단 메뉴얼을 보시면, C API가 어떻게 되어 있는지 자세히(?) 나와
있습니다. C++은 메뉴얼을 보면 두가지 프로젝트가 있다는데, 지금
이상하게 MySQL 웹사이트가 접속이 안 되서 저도 확인은 못 하고 있
습니다. (언제쯤이나 될련지.. --;;)
밑의 건 진짜 간단한 예제입니다. 참고가 됐으면 좋겠군요. ^^
DB 테이블 구조는 id하고 name만 있는 거에요.. :)
Output이 어떻게 나오는지는 집적 해 보세요...
(컴파일할 때 워닝이 하나 뜨는데 잘 안 잡히는군요. --;)
#include <mysql/mysql.h>
#include <stdio.h>
int main() {
MYSQL *mysql;
MYSQL_RES *result;
MYSQL_ROW rows;
MYSQL_FIELD *field;
char *host = "localhost";
char *db = "test";
char *user = "sizer";
char *password = "";
unsigned int num_fields;
unsigned int num_rows;
unsigned int *lengths;
unsigned int i;
int ping_status;
mysql = mysql_init(NULL);
if (!mysql)
printf("Out of Memory
");
else
printf("Success initialize MySQL
");
mysql = mysql_connect(mysql, host, user, password);
if (mysql)
printf("Success connect MySQL DB Server
");
else
printf("Fail connect MySQL DB Server
");
printf("This MySQL Client Version is %s
", mysql_get_client_info());
printf("This MySQL Host is %s
", mysql_get_host_info(mysql));
printf("This MySQL Protocol Version is %u
", mysql_get_proto_info(mysql));
printf("This MySQL Server Version is %s
", mysql_get_server_info(mysql));
if (!(mysql_select_db(mysql, db)))
printf("Success change %s Database
", db);
else
printf("Fail change %s Database
", db);
if (mysql_query(mysql, "select * from Test")) {
printf("Fail query to %s
", db);
exit(1);
}
else
printf("Success query to %s
", db);
result = mysql_store_result(mysql);
num_fields = mysql_num_fields(result);
printf("Fields number is %u
", num_fields);
num_rows = mysql_affected_rows(mysql);
printf("Rows number is %u
", num_rows);
printf("This is Field Structure & Rows of Table Test in test Databases
");
while((field = mysql_fetch_field(result))) {
printf("[%s] ", field->name);
}
printf("
");
while ((rows = mysql_fetch_row(result))) {
lengths = mysql_fetch_lengths(result);
for (i = 0; i < num_fields; i++) {
printf("[%.*s] ", (int)lengths, rows ? rows : "NULL");
}
printf("
");
}
mysql_free_result(result);
printf("Query results is freed
");
if (!(ping_status = mysql_ping(mysql)))
printf("Ping Result: Server is connected
");
else
printf("Ping Result: Server is not connected
");
mysql_close(mysql);
printf("Teminate Server connect
");
if (!(ping_status = mysql_ping(mysql)))
printf("Ping Result: Server is connected
");
else
printf("Ping Result: Server is not connected
");
return 0;
}
|