c/c++ 实现直接连接查询mysql 源码

#ifdef WIN32
#include
#include
#include "mysql.h"
#pragma comment(lib, "libmySQL.lib")
#endif

int sqlsm()
{
//必备的一个数据结构  
 
MYSQL * con; //= mysql_init((MYSQL*) 0); 
MYSQL_RES *res;
MYSQL_ROW row;
 
 
char tmp[400];
 
//database configuartion
char dbuser[30]="root"; 
char dbpasswd[30]="root";
char dbip[30]="127.0.0.1";
char dbname[16]="stop_park1";
char tablename[50]="tc";
//char *query="smart_in_ip";;
char qbuf[60];   //存放查询sql语句字符串

int x;
int y;
int rt;//return value
unsigned int t;
 
int count = 0;
 
 
//printf("input x,y\n");
//scanf("%d,%d",&x,&y);
//fflush(stdin);
//printf("input over\n");
con = mysql_init((MYSQL*) 0); 
 




if ( con !=NULL && mysql_real_connect(con,dbip,dbuser,dbpasswd,dbname,3306/*TCP IP端口*/,NULL/*Unix Socket 连接类型*/,CLIENT_ODBC/*运行成ODBC数据库标志*/) ) 

if (!mysql_select_db(con,dbname)) 

printf("Select successfully the database!\n"); 
        
con ->reconnect = 1; 
 
sprintf(tmp,"select smart_in_ip  from %s",tablename);
rt=mysql_real_query(con,tmp,strlen(tmp));
if (rt)
{
printf("Error making query: %s !!!\n",mysql_error(con));
}
else
{
printf("query %s succeed!\n",tmp);
}
        
}

res = mysql_store_result(con);//将结果保存在res结构体中


while(row = mysql_fetch_row(res))  
{  
/** 
* MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result); 
  * 检索行 
*/  


for(t=0;t {  
printf("%s  ",row[t]);  
}  
printf(".............\n");  
count ++;
}  
printf("number of rows %d\n",count);
printf("mysql_free_result...\n");  
mysql_free_result(res);  


mysql_close(con);
}
 
else
{
MessageBoxA(NULL,"Unable to connect the database,check your configuration!","",NULL);
 
}


return 0;
}

你可能感兴趣的:(mysql)