#include
#include
#include
#include
#include "include/mysql.h"
#pragma comment(lib,"libmySQL.lib")
using namespace std;
int main()
{
int ret;
char column[32][32];
char datatable[255];
MYSQL MysqlTemp;
MYSQL_RES * result;
MYSQL_FIELD *fd;
MYSQL_ROW sql_row;
/***************************************初始化数据库******************************/
mysql_init(&MysqlTemp);
/***************************************链接数据库******************************/
if(!mysql_real_connect(&MysqlTemp,"localhost","root","123456","test",3306,NULL,0))
{ //printf("Mysql conect fail!");
//AfxMessageBox(_T("数据库连接失败!"));
MessageBox(NULL,"数据库连接失败!","消息",MB_OK);
mysql_close(&MysqlTemp);
return -1;
}
/***************************************选定数据库******************************/
if(mysql_select_db(&MysqlTemp,"test"))
{
MessageBox(NULL,"选择数据库失败!","消息",MB_OK);
return -1;
}
/***************************************创建数据库******************************/
//sprintf(datatable,"CREATE database temp2;");
//mysql_query(&MysqlTemp,datatable);//查询
/***************************************选定数据库******************************/
sprintf(datatable,"use temp2;");
mysql_query(&MysqlTemp,datatable);//查询
/***************************************选定数据库 功能同上******************************/
//if(mysql_select_db(&MysqlTemp,"temp2"))
//{
// MessageBox(NULL,"选择数据库失败!","消息",MB_OK);
// return -1;
//}
/***************************************在数据库新建数据表******************************/
sprintf(datatable,"create table if not exists MyClass(id int(4) NOT NULL auto_increment,\
name char(20) NOT NULL,\
sex int(4) NOT NULL default '0',\
degree int(4),\
PRIMARY KEY(id))ENGINE=InnoDB;");
mysql_query(&MysqlTemp,datatable);
if(mysql_errno(&MysqlTemp)==1050)//查询
{
MessageBox(NULL,"数据表已存在!","消息",MB_OK);
return -1;
}
//memset(datatable,0,255);
/***************************************在表中插入数据******************************/
sprintf(datatable,"insert into MyClass values(7,'Tom',96,89),(8,'Jame',44,67),(9,'Honey',32,12);");
if(mysql_query(&MysqlTemp,datatable))//查询
{
MessageBox(NULL,"插入数据失败!","消息",MB_OK); return -1;
}
sprintf(datatable,"SELECT * FROM MyClass;");
ret = mysql_query(&MysqlTemp,datatable);
if (!ret)
{
result = mysql_store_result(&MysqlTemp);
if (result)
{
int i,j;
cout<
//获取具体的查询结果的列名,如果没有指定字段偏移量,则下一个尚未被 mysql_fetch_field() 取得的字段被提取。
for(i=0;fd = mysql_fetch_field(result);i++)
{
strcpy(column[i],fd->name);
}
//获取具体的查询结果的列数
j = mysql_num_fields(result);
for(i=0;i
{
printf("%s\t",column[i]);
}
printf("\n");
//获取具体的查询结果的行,检索一个结果集合的下一行
while (sql_row = mysql_fetch_row(result))
{
for (i=0;i
{
printf("%s\t",sql_row[i]);
}
printf("\n");
}
}
}
return 0;
}