Operate mysql in linux and support reconnection to MySQL

Source: Internet
Author: User
Operate mysql in linux and support reconnecting bitsCN.com

(1) code

# Include

# Include

# Include

# Include

# Include

# Include

# Define MAX_QUERY_LEN 1024

# Define MAX_QUERY_COUNT 2

# Define PRINT_MYSQL_ERROR (mysql) fprintf (stderr, "mysql-error: % s/n", mysql_error (mysql ))

Typedef struct prop_global {

MYSQL * mysql;

Char * db_host;

Char * db_user;

Char * db_pwd;

Char * db_name;

Char query [MAX_QUERY_LEN];

Int order_id;

} Prop_global_t;

Static prop_global_t * g_p;

Int prop_init (const char * host, const char * user, const char * pwd, const char * db ){

Prop_global_t * y = (prop_global_t *) malloc (sizeof (* y ));

Y-> mysql = mysql_init (NULL );

If (NULL = y-> mysql ){

PRINT_MYSQL_ERROR (y-> mysql );

Return-1;

}

// Printf ("mysql % s, % s/n", host, user, pwd, db );

If (NULL = mysql_real_connect (y-> mysql, host, user, pwd, db, 0, NULL, 0 )){

PRINT_MYSQL_ERROR (y-> mysql );

Return-1;

}

Y-> db_host = strdup (host );

Y-> db_user = strdup (user );

Y-> db_pwd = strdup (pwd );

Y-> db_name = strdup (db );

Y-> order_id = 1;

G_p = y;

// Printf ("mysql init OK/n ");

Return 0;

}

Int safe_query (prop_global_t * y, const char * query, int len ){

Int I;

Int errno;

// Printf ("SQL: % d % s/n", len, query );

For (I = 0; I

Errno = mysql_real_query (y-> mysql, query, len );

Switch (errno ){

Case 0: return 0;

Case CR_SERVER_GONE_ERROR:

Case CR_SERVER_LOST:

Mysql_close (y-> mysql );

Y-> mysql = mysql_init (NULL );

If (NULL = mysql_real_connect (y-> mysql, y-> db_host, y-> db_user, y-> db_pwd, y-> db_name, 0, NULL, 0 )){

PRINT_MYSQL_ERROR (y-> mysql );

Return-1;

}

Break;

Default:

PRINT_MYSQL_ERROR (y-> mysql );

Return-1;

}

}

Return-1;

}

Int get_lzddz_num (void ){

Int n;

Int rownum = 0;

// MYSQL_ROW row;

MYSQL_RES * res = NULL;

Prop_global_t * y = g_p;

Int r =-1;

N = snprintf (y-> query, MAX_QUERY_LEN, "select userid, matchid, matchseq, userseq, score, state, takecoin, token, signupcond, site, rtime from t_jj_user_match where matchid = 6684674 or matchid = 6684675 or matchid = 6684676 ");

If (safe_query (y, y-> query, n )){

Printf ("err: % d/n", _ LINE _); return r;

}

Res = mysql_store_result (y-> mysql );

If (NULL = res ){

Printf ("err: % d/n", _ LINE _); return r;

}

Rownum = mysql_num_rows (res );

/*

Printf ("lzddz num = % d/n", rownum );

If (rownum> 0 ){

While (row = mysql_fetch_row (res ))){

Printf ("% d/t", atoi (row [0]); // userid

Printf ("% d/t", atoi (row [1]); // matchid

Printf ("% d/t", atoi (row [2]); // matchseq

Printf ("% d/t", atoi (row [3]); // userseq

Printf ("% d/t", atoi (row [4]); // score

Printf ("% d/t", atoi (row [5]); // state

Printf ("% d/t", atoi (row [6]); // takecoin

Printf ("% d/t", atoi (row [7]); // token

Printf ("% d/t", atoi (row [8]); // signupcond

Printf ("% d/t", atoi (row [9]); // site

Printf ("% d/t", atoi (row [10]); // rtime

Printf ("/n ");

}

}

*/

Mysql_free_result (res );

System ("date ");

Printf ("lzddz num = % d/n", rownum );

Return rownum;

}

Void prop_release (void ){

Prop_global_t * y = g_p;

Mysql_close (y-> mysql );

Free (y-> db_host );

Free (y-> db_user );

Free (y-> db_pwd );

Free (y-> db_name );

}

Int main (void ){

Int ret = 0;

Ret = prop_init ("41.15.11.59", "lhm", "1234567lhm", "db_jj ");

If (0 = ret ){

Get_lzddz_num ();

Prop_release ();

}

Return 0;

}

(2) Makefile

[Lvhongmin @ ay140142536089361adz database] $ cat Makefile

FLAGS =-O2-g-Wpointer-arith-Wstrict-prototypes-lm-W-Wall

LIB =-lpthread-lmysqlclient

Export de_path =/usr/include/mysql

CC = gcc

All: lhm.exe

Lhm.exe: lhm_query.c

$ (CC)-o $ {LIB }$ {FLAGS}-L/usr/lib64/mysql-I $ {INCLUDE_PATH} lhm_query.c

Clean:

Rm lhm.exe

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.