(a) Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <mysql/errmsg.h>
#include <sys/time.h>
#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,%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<max_query_count; 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, SIGNU Pcond,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;
}
(ii) Makefile
[email protected] database]$ cat Makefile
FLAGS =-o2-g-wpointer-arith-wstrict-prototypes-lm-w-wall
LIB =-lpthread-lmysqlclient
include_path =/usr/include/mysql
CC = gcc
All:lhm.exe
Lhm.exe:lhm_query.c
$ (CC)-o [email protected] ${lib} ${flags}-l/usr/lib64/mysql-i ${include_path} lhm_query.c
Clean
RM Lhm.exe