#include <windows.h>
#include <iostream>
#include <Mysql.h> //文件位于 Mysql 提供的 C API 目录中
using namespace std;
// linux 等系统中请加入 -lMysql
#pragma comment( lib, "libMysql.lib")
/*****************************************************************/
///name : main
//function : 主测试函数
//access : private
//para :
// 1. : int argc
// : 系统参数个数
// 2. : char * argv[]
// : 参数数值
//return : 返回给 startup 函数的退出参数
//author : hzh
//date : 2006-06-24
/*****************************************************************/
int main( int argc, char * argv[] )
{
Mysql mydata;
//初始化数据结构
if(Mysql_init(&mydata) == NULL)
{
std::cout<<"init Mysql data stauct fail"<<endl;
return -1;
}
//连接数据库
if(argc == 1)
{
if(NULL == Mysql_real_connect(&mydata,"127.0.0.1","root","Mysql5",
"hzhdb",Mysql_PORT,NULL,0))
{
std::cout<<"connect database fail"<<endl<<Mysql_error(&mydata)<<endl;
return -1;
}
}else if(argc == 5)
{
if(NULL == Mysql_real_connect(&mydata,argv[1],
argv[2],argv[3],argv[4],Mysql_PORT,NULL,0))
{
std::cout<<"connect database fail"<<endl<<Mysql_error(&mydata)<<endl;
return -1;
}
}
else
{
std::cout<<"run parameter error"<<endl;
return -1;
}
std::string s_sql = "drop table hzhtest";
if(Mysql_query(&mydata,s_sql.c_str()) != 0)
{
//删除表失败
Mysql_close(&mydata);
std::cout<<"drop table fail"<<endl<<Mysql_error(&mydata)<<endl;
}
//创建数据表,字段 myid 设置了自增列属性
s_sql = "create table hzhtest(";
s_sql += "myid integer not null auto_increment,";
s_sql += "mytime datetime null,myname varchar(30),";
s_sql += " primary key(myid))";
if(Mysql_query(&mydata,s_sql.c_str()) != 0)
{
//创建表失败
Mysql_close(&mydata);
std::cout<<"create table fail"<<endl
<<Mysql_error(&mydata)<<endl;
return -1;
}
//向表中插入数据
for(int k = 1; k < 30; ++k)
{
s_sql = "insert into hzhtest(mytime,myname) values";
s_sql += "("2006-06-";
char buff[20];
memset(buff,0,sizeof(buff));
itoa(k,buff,10);
s_sql += buff;
s_sql += " ";
int i = k % 3;
memset(buff,0,sizeof(buff));
itoa(i,buff,10);
s_sql += buff;
s_sql += ":01:01"";
if(i == 0)
{
s_sql += ",NULL";
}
else
{
s_sql += ","黄志辉";
s_sql += buff;
s_sql += """;
}
s_sql += ")";
if(Mysql_query(&mydata,s_sql.c_str()) != 0)
{
//执行SQL语句出错
std::cout<<"execute insert syntax fail"<<
endl<<Mysql_error(&mydata)<<endl;
Mysql_close(&mydata) ;
return -1;
}
}
//查询数据并显示
s_sql = "select myid,mytime,myname from hzhtest";
if(Mysql_query(&mydata,s_sql.c_str()) != 0)
{
//执行SQL语句出错
Mysql_close(&mydata);
std::cout<<"execute sql syntax fail"<<
endl<<Mysql_error(&mydata)<<endl;
return -1;
}
Mysql_RES *result = Mysql_store_result(&mydata);
//取得查询结果
int rowcount = Mysql_num_rows(result);
//取得有效记录数
std::cout<<"exec sql: "<<s_sql.c_str()<<",row count: "<<rowcount<<endl;
Mysql_FIELD *fields = NULL;
//取得各字段名
for(int i = 0; fields = Mysql_fetch_field(result);++i)
{
std::cout<<fields->name<<" ";
}
std::cout<<endl;
//依次读取各条记录
Mysql_ROW currrow = NULL;
while((currrow = Mysql_fetch_row(result)) != NULL)
{
//读行的记录
for(int i = 0; i < Mysql_num_fields(result); ++i)
{
std::cout<<(currrow[i] ? currrow[i] : "NULL")<<" ";
}
std::cout<<endl;
}
Mysql_free_result(result) ;
Mysql_close(&mydata);
system("pause");
return 1;
}