voidCInsertDlg::OnBnClickedOk(){//TODO:在此添加控件通知处理程序代码UpdateData(true);MYSQLmysql;mysql_init(&mysql);if(!mysql_real_connect(&mysql,"localhost","root","123456...
void CInsertDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
MYSQL mysql;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost","root","123456","workerdb",3306,NULL,0))
{
int re = AfxMessageBox(_T("数据库连接失败!"),MB_OK);
}
CString strSQL;
CString tstr;
tstr.Format(_T("%s"),"select * from workertable");
//tstr = "select * from workertable";
int a = mysql_query(&mysql, (char*)(LPCTSTR) tstr);
MYSQL_RES* rs = mysql_store_result(&mysql);
int Rows = mysql_num_rows(rs);
Rows += 1;
strSQL.Format(_T("insert into workertable values(%d,\'%s\',%d,%d,%d)"),Rows,m_Name,m_Ma1,m_Ma2,0);
if (mysql_real_query(&mysql,(char*)(LPCTSTR)strSQL, (UINT)strSQL.GetLength())!=0)
{
AfxMessageBox(strSQL);
//AfxMessageBox(_T("添加失败!"),MB_OK);
}
/*strSQL = "insert into workertable values(10,'leng',11,12,0)";
if (mysql_real_query(&mysql,"insert into workertable values(10,'leng',11,12,0)",(UINT) strSQL.GetLength())!=0)
{
AfxMessageBox(strSQL,MB_OK);
//AfxMessageBox(_T("添加失败!"),MB_OK);
}*/
mysql_close(&mysql);
CDialogEx::OnOK();
}
这个函数中第一次调用mysql_real_query时会添加失败。
当用后面注释掉的那部分代码进行mysql_real_query时可以向数据库中添加记录。
另外,用前面一个messagebox中打印出的strSQL直接在mysql中输入是可以添加记录的。。。求指教啊~。。。。~
展开