/*****************************************************************
以下是研究 mysql 5.0 得出的结果,描述并使用标准 c++演示了使用 MySQL
C API 函数 简单操作数据库的流程;
例子程序在 VC6 + windows 2000 上调试通过
*****************************************************************/
#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<<"\t\t";
}
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")<<"\t";
}
std::cout<<endl;
}
mysql_free_result(result) ;
mysql_close(&mydata);
system("pause");
return 1;
}