mysql c-api를 사용 중 해결이 안되네요.
글쓴이: syan / 작성시간: 일, 2003/02/09 - 3:21오후
한개의 소스안에서 그냥 mysql c-api를 사용해서 순서대로 나열을 할 때에는 수행이 잘 되었는데요.
이걸 라이브러리처럼 묶어 볼려구 아래의 소스처럼 하니깐 실패가 납니다.
MYSQL *
connectDB(const char *ip, const char *id, const char *pass, const char *db)
{
MYSQL mysql, *con;
mysql_init(&mysql);
con = mysql_connect(&mysql, ip, id, pass);
if (!con) {
MYSQLERROR(connectDB, con);
}
if (mysql_select_db(con, db)) {
MYSQLERROR(connectDB, con);
}
return con;
}
void
queryDB(MYSQL **con, const char *str)
{
unsigned int len = strlen(str);
int rv;
rv = mysql_real_query(*con, str, len);
if (rv) {
MYSQLERROR(queryDB, *con);
}
}
unsigned long
getQueryResult(MYSQL **con, MYSQL_RES **res)
{
unsigned long rows;
*res = mysql_store_result(*con);
if (!*res) {
MYSQLERROR(getQueryResult, *con);
}
rows = mysql_num_rows(*res);
return (rows);
}
MYSQL 형과 MYSQL_RES 형은 함수 수행중에 값이 바뀔것 같아서 이중포인터로 넘겨줬고요. 제가 보기엔 문제가 없을 것 같은데, 같은 db에 같은 쿼리를 날려도 에러가 나네요.
일단 에러는 mysql_real_query()에서 -1을 리턴하면서 MySQL server has gone away의 에러를 냅니다.
Forums:


RTFM...
MYSQL * connectDB(const char *ip, const char *id, const char *pass, const char *db) { MYSQL mysql, *con; mysql_init(&mysql); con = mysql_connect(&mysql, ip, id, pass); if (!con) { MYSQLERROR(connectDB, con); } if (mysql_select_db(con, db)) { MYSQLERROR(connectDB, con); } return con; }디버거로 확인해 보시면 아시겠지만, con == &mysql 입니다... 당연히 return하고 나면 con은 invalid한 값입니다.
document을 확인해보세요....
http://www.mysql.com/doc/en/mysql_connect.html
http://www.mysql.com/doc/en/mysql_real_connect.html
Consider the ravens: for they neither sow nor reap; which neither have storehouse nor barn; and God feedeth them: how much more are ye better than the fowls?
Luke 12:24
Re: mysql c-api를 사용 중 해결이 안되네요.
MYSQL * connectDB(const char *ip, const char *id, const char *pass, const char *db) { MYSQL *mysql; mysql = mysql_init (NULL); if (!mysql) { /* TODO: 에러처리 */ return NULL; } if (NULL == mysql_connect(mysql, ip, id, pass)) { MYSQLERROR(connectDB, mysql); mysql_close (mysql); } if (mysql_select_db(mysql, db)) { MYSQLERROR(connectDB, mysql); mysql_close (mysql); } return mysql; }이렇게 써보세요.
screen + vim + ctags 좋아요~
댓글 달기