⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqltestdlg.cpp

📁 介绍了MySQL的使用、调试和调用的方法和接口
💻 CPP
字号:
// MySQLTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MySQLTest.h"
#include "MySQLTestDlg.h"


#include "stdio.h"
#include "c:\\mysql\include\\mysql.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMySQLTestDlg dialog

CMySQLTestDlg::CMySQLTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMySQLTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMySQLTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMySQLTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMySQLTestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMySQLTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMySQLTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDMySQLTest, OnMySQLTest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMySQLTestDlg message handlers

BOOL CMySQLTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMySQLTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMySQLTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMySQLTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

MYSQL		*conn;
MYSQL_RES	*res;
MYSQL_ROW	row;

//void set(unsigned char **p)
//{
//	unsigned char *aaa;
//	aaa = (unsigned char*)malloc(100);
//	char aa[100] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
//	memcpy(aaa,aa,100);
//
//	*p = aaa;
//}

void process_result_set(MYSQL *conn, MYSQL_RES *res_set)
{
	char msg[40960]="----------------------------------------------------------------------------\nID\tName\tSex\tBirth\t\tAddress\n----------------------------------------------------------------------------\n";
	unsigned int i,j=27+78+76;
	while((row = mysql_fetch_row(res_set)) != NULL)
	{
		for(i=0;i<mysql_num_fields(res_set);i++)
		{
			if(i>0)
			{
				strcat(msg,"\t");
				j+=1;
			}
			sprintf(msg+j,"%s",row[i]!=NULL?row[i]:"NULL");
			
			j = j+ (row[i]==NULL?4:(strlen(row[i])));
			
		}
		
		strcat(msg,"\n");
		j+=1;
	}
	
	if(mysql_errno(conn)!=0)
		AfxMessageBox("conn fetch error!");
	else
	{
		strcat(msg,"----------------------------------------------------------------------------\n\n");
		j+=78;
		
		sprintf(msg+j,"All Record Number is %d。",res_set->row_count);
		
		AfxMessageBox(msg);
	}
}


void CMySQLTestDlg::OnMySQLTest() 
{
	conn = mysql_init(NULL);
	if(conn==NULL)
	{
		AfxMessageBox("初始化失败!");
		return;
	}
	if(mysql_real_connect(
		conn,
		"localhost",			//Host
		"root",					//user name
		"ldb1234",				//password
		"sql_query",			//使用的数据库名称
		0,
		NULL,
		0)
		==NULL)
	{
		AfxMessageBox("数据库连接失败!");
		return;
	}
	
	//if(mysql_query(conn, "select name,sex,birth,address,id from person where length(name)<8;")!=0)
	//if(mysql_query(conn, "select name,sex,birth,address from student;")!=0)
	//if(mysql_query(conn, "describe student;")!=0)
	
	for(int i=1; i<=0; i++)
	{
		char aaa[256]="";
		if (i%2)
			sprintf(aaa, "insert into ppp values(NULL,\"aaaaa\",1, %4d%02d%02d, \"aaaaaaaaaa\",\"%06d\");",(i%10000==0)?1:(i%10000),(i%13==0)?1:(i%13),(i%32==0)?1:(i%32),i);
		else
			sprintf(aaa, "insert into ppp values(NULL,\"aaaaa\",2, %4d%02d%02d, \"aaaaaaaaaa\",\"%06d\");",(i%10000==0)?1:(i%10000),(i%13==0)?1:(i%13),(i%32==0)?1:(i%32),i);
		if(mysql_query(conn, aaa)!=0)
		{
			AfxMessageBox("数据库查询失败!");
			return;
		}
	}
	
	//{
	//	mysql_close(conn);
	//	AfxMessageBox("数据库操作成功!");
	//}
	
	int tmp = mysql_query(conn,"select id,name,sex,birth,address from ppp where name=\"bbbbb\";");
	res = mysql_store_result(conn);
	//unsigned long * length;
	//length = mysql_fetch_lengths(res);
	/*
	else
	{
		mysql_close(conn);
		AfxMessageBox("数据库连接成功!");
	}
	*/
	process_result_set(conn,res);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -