#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 += " ";