// TestMysql.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
#pragma comment(lib,"libmysql.lib")//连接MysQL需要的库
using namespace std;
//---------------------------------- 此函数用了静态的MySql对象,会爆出标题错误------------------------
void testJJ()
{
const char user[] = "dcsdba"; //username
const char pswd[] = "Office2012"; //password
const char host[] = "192.168.5.25"; //or"127.0.0.1"
const char table[] = "dcs"; //database
unsigned int port = 3306; //server port
MYSQL mysql;
my_bool reconn;
if (mysql_init(&mysql) == NULL) {
fprintf(stderr, "failed to initialize mysql\n");
exit(EXIT_FAILURE);
}
mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconn);
if (mysql_real_connect(&mysql, host, user, pswd, table,
port, NULL, 0) == NULL) {
fprintf(stderr, "failed to connect mysql server\n");
exit(EXIT_FAILURE);
}
char query[512];
int qlen = 0;
if (mysql_real_query(&mysql, "select * from t_fact_data", strlen("select * from t_fact_data")))
{
}
MYSQL_RES *res;
MYSQL_ROW row;
long num_fields;
MYSQL_FIELD *fields;
int i;
if ((res = mysql_use_result(&mysql)) == NULL) {
fprintf(stderr, "mysql_store_result: %s\n", mysql_error(&mysql));
exit(EXIT_FAILURE);
}
num_fields = mysql_num_fields(res);
printf("num fields: %lu\n", num_fields);
fields = mysql_fetch_fields(res);
for (i = 0; i < num_fields; i++)
printf("%s ", fields[i].name);
printf("\n");
unsigned long *lengths;
while ((row = mysql_fetch_row(res)) != NULL) {
lengths = mysql_fetch_lengths(res);
for (i = 0; i < num_fields; i++)
printf("%s(%ld) ", row[i], lengths[i]);
printf("\n");
}
mysql_free_result(res);
mysql_close(&mysql);
mysql_library_end();
}
//----------- 此函数用了动态的MySql对象即由mysql_init自动分配,不会再爆出错误-----------
void mytest()
{
const char user[] = "dcsdba"; //username
const char pswd[] = "Office2012"; //password
const char host[] = "192.168.5.25"; //or"127.0.0.1"
const char table[] = "dcs"; //database
unsigned int port = 3306; //server port
MYSQL *myCont;
MYSQL_RES *result = NULL;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[32][32];
int cnt;
myCont = mysql_init(NULL);
if (mysql_real_connect(myCont, host, user, pswd, table, port, NULL, 0))
{
cout << "connect succeed!" << endl;
mysql_query(myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
cnt = mysql_query(myCont, "select * from t_fact_data");//查询
if (!cnt)
{
result = mysql_store_result(myCont);//保存查询到的数据到result
if (result)
{
int i, j;
cout << "number of result: " << (unsigned long)mysql_num_rows(result) << endl;
for (i = 0; fd = mysql_fetch_field(result); i++)//获取列名
{
memcpy(column[i], fd->name, strlen(fd->name));
printf("%s\t", fd->name);
}
j = mysql_num_fields(result);
printf("\r\n");
while (sql_row = mysql_fetch_row(result))//获取具体的数据
{
for (i = 0; i < j; i++)
{
printf("%s\t", sql_row[i]);
}
printf("\r\n");
}
}
}
else
{
cout << "query sql failed!" << endl;
}
}
else
{
cout << "connect failed!" << endl;
}
if (result != NULL)
{
mysql_free_result(result);//释放结果资源
}
mysql_close(myCont);//断开连接
mysql_library_end();
}
int main()
{
mytest();
testJJ();
return 0;
}