본문 바로가기

DATABASE

[MYSQL] C library

mysql library.
번호: 74 / 작성자: jinyedge / 등록일: 2004-01-05 16:17:36 / 조회: 193
/*
This is a program demonstrating how to use my old mysql library.
I think I wrote the library 2 or 3 years ago. And I think it might be
still useful to somebody else. If anybody finds it useful just use it.
I'll be very pleased.

To run this program we need a table in mysql's test database
like below.

create table temp(no int, name varchar(100));

2004.01.04.
written by jinyedge.
*/

#include <stdio.h>
#include "/usr/local/mysql/include/mysql.h"

/*-------------------*/
typedef struct _RecordSet{
MYSQL_RES* res;
MYSQL_ROW row;
} RecordSet;

MYSQL mysql;

/*-------------------*/
/*success = 1, error = 0*/
int db_conn(char* host, char* user, char* pw, char* db){
mysql_init(&mysql);
if(! mysql_real_connect(&mysql, host, user, pw, db, 0, (char *)NULL, 0)){return 0;}
return 1;
}

/*-------------------*/
void db_close(){
mysql_close(&mysql);
}

/*-------------------*/
/*success = 1, error = 0*/
int db_one_str(char* q, char buf[]){
MYSQL_RES* res;
MYSQL_ROW row;
if(mysql_real_query(&mysql, q, strlen(q)) != 0){
strcpy(buf, "");
return 0;
}
res = mysql_store_result(&mysql);
row = mysql_fetch_row(res);
mysql_free_result(res);

strcpy(buf, row[0]);
return 1;
}

/*-------------------*/
/*success = 1, error = 0*/
int db_exec(char *q){
if(mysql_real_query(&mysql, q, strlen(q)) != 0){return 0;}
return 1;
}

/*-------------------*/
/*error = NULL*/
RecordSet* rs_open(char *q){
RecordSet* rs;
rs = (RecordSet *)malloc(sizeof(RecordSet));
if(mysql_real_query(&mysql, q, strlen(q)) != 0){
return NULL;
}
rs->res = mysql_store_result(&mysql);
return rs;
}

/*-------------------*/
int rs_next(RecordSet* rs){
if((rs->row = mysql_fetch_row(rs->res)) == NULL){return 0;}
return 1;
}

/*-------------------*/
void rs_get_str(RecordSet* rs, int idx, char buf[]){
strcpy(buf, rs->row[idx]);
}

/*-------------------*/
void rs_close(RecordSet* rs){
mysql_free_result(rs->res);
free(rs);
}

/*-------------------*/
int main(void){
char no[255], name[255];
char* q = "";
RecordSet* rs = NULL;

/*Connect to mysql.*/
if(! db_conn("localhost", "root", "", "test")){
puts("db_conn failed!");
exit(-1);
}

/*Insert some data.*/
q = "insert into temp values(1, '홍길동')";
db_exec(q);
q = "insert into temp values(2, '고길동')";
db_exec(q);
q = "insert into temp values(3, '최길동')";
db_exec(q);

/*Select one string.*/
q = "select name from temp where no = 2";
db_one_str(q, no);
printf("%s\n%s\n\n", q, no);

/*RecordSet.*/
q = "select * from temp";
rs = rs_open(q);
while(rs_next(rs)){
rs_get_str(rs, 0, no);
rs_get_str(rs, 1, name);
printf("no = %s, name = %s\n", no, name);
}
rs_close(rs);

/*Delete data.*/
q = "delete from temp";
db_exec(q);

/*Close connection.*/
db_close();

return 0;
}

'DATABASE' 카테고리의 다른 글

[Tech Report] - Hybrid MM DBMS `ALTIBASE 4`  (0) 2011.10.16
MMDBMS의 활용가능 분야와 활용 방법  (0) 2011.10.16
연재/하이브리드 MM DBMS  (0) 2011.10.16
[MYSQL] c api 예제  (0) 2011.10.16
[MYSQL] c api 예제  (0) 2011.10.16