#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/shm.h>
#include <mysql.h>
extern MYSQL *STDCALL mysql_init(MYSQL *mysql);
extern MYSQL *STDCALL mysql_real_connect(MYSQL *mysql, const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag);
extern const char * STDCALL mysql_error(MYSQL *mysql);
extern int STDCALL mysql_select_db(MYSQL *mysql, const char *db);
extern unsigned int STDCALL mysql_errno(MYSQL *mysql);
extern void STDCALL mysql_close(MYSQL *sock);
extern int STDCALL mysql_query(MYSQL *mysql, const char *q);
#ifdef MYSQL_ERROR
#endif
MYSQL mysql;
char mysql_log_msg[SZ_MAX];
/*- ------------------------------------------------ -*
*- ConnectDB
*- ------------------------------------------------ -*/
int ConnectDB(char *database)
{
FILE *fp;
char conn[20], user[20], passwd[20];
char buff[100], mysql_file[200];
if((fp= fopen("MYSQLDATABASE.dat", "r")) == NULL)
{
return(-1);
}
while(fgets(buff,sizeof(buff),fp))
{
if(strncmp(buff, "#", 1) == 0 || strncmp(buff, "\n", 1) == 0) continue;
sscanf(buff, "%s %s %s", &conn, &user, &passwd);
}
fclose(fp);
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, conn, user, passwd, database, 0, NULL, 0))
{
return(-2);
}
printf("CONNECT DATABASE\n");
if(mysql_select_db(&mysql, database) != 0)
{
return(-3);
}
return(0);
}
/*- ------------------------------------------------ -*
*- DATABASE 연결해제 함수
*- ------------------------------------------------ -*/
DisconnectDB()
{
mysql_close(&mysql);
printf("DISCONNECT DATABASE\n");
}
/*- ------------------------------------------------ -*
*- MYSQL Table Query function
*- ------------------------------------------------ -*/
int MysqlQuery(char *query)
{
FILE *fp=NULL;
char status_file[SZ_MAX], server_status[20];
mysql_query(&mysql, "Set names euckr;");
if((fp= fopen("SERVER.dat", "r")) == NULL)
{
return(-1);
}
while(fgets(server_status,sizeof(server_status),fp))
{
if(strncmp(server_status, "#", 1) == 0 || strncmp(server_status, "\n", 1) == 0) continue;
}
fclose(fp);
//[LIVE] is all ok(INSERT,TRUNCATE,UPDATE,REPLACE,DELETE,SELECT etc),
//[STANDBY] is SELECT ok
if( (strncmp(server_status, "LIVE", 4) == 0) || ((strncmp(server_status, "STANDBY", 7) == 0)
&& ((strncmp(query, "SELECT", 6) == 0) || (strncmp(query, "(SELECT", 7) == 0))))
{
if(mysql_query(&mysql, query) != 0)
{
sprintf(mysql_log_msg, "MYSQL ERROR[%s-%d]\n", mysql_error(&mysql), mysql_errno(&mysql));
if(mysql_errno(&mysql) != 1062)
{
#ifdef MYSQL_ERROR
printf("MYSQL ERROR[%s-%s]\n", query, mysql_log_msg);
#endif
printf("%s\n", mysql_log_msg);
}
}
return(mysql_errno(&mysql));
}
return(0);
}
'데이타베이스 > MySQL' 카테고리의 다른 글
FUNCTION 생성및 활용 (0) | 2019.10.31 |
---|---|
LINUX,프로그래밍을 통한 PROCEDURE 호출후 결과 (0) | 2019.10.30 |
#LINUX,send_msg_to_queue.c (2번째) (0) | 2019.10.30 |
#LINUX,recv_msg_from_queue.c (두번째) (0) | 2019.10.30 |
#Queue를 이용한 MySQL입력 성능향상에 관한 글(1) (0) | 2019.10.30 |