源代码:
void mima::OnBnClickedOk()
{
MYSQL mysql; //
MYSQL_RES *result; //定义结果集变量
MYSQL_ROW row; //定义行变量
char ch[2];
//char id[10]; /*结果集中的行数*/
char *sql;
char dest[500] = { " " };
char dest1[500] = { " " };
char *bookname;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "localhost", "root", "556677", "db_book", 3306, NULL, 0))
{
MessageBox(L"连接数据库失败", L"提示");
}
else
{
char * ttt;
CString str = id1;//,str1,str2;
LPCTSTR p = str.GetBuffer(0);
str.ReleaseBuffer();
ttt = new char[str.GetLength() + 1];
strcpy_s(ttt, str.GetLength() + 1, CT2CA(p));
sql = "select * from tb_user where NUM=";
strcat(dest, sql);
strcat(dest, ttt); /*将图书编号追加到sql语句后面*/
if (mysql_query(&mysql, "select * from tb_user"))
{ //如果查询失败
MessageBox(L"查询 tb_user 数据表失败!", L"提示");
}
else
{
result = mysql_store_result(&mysql); //获得结果集
if (mysql_num_rows(result) != NULL)
{
strcpy(ch, "Y");
if (strcmp(ch, "Y") == 0) /*判断是否需要录入*/
{
row = mysql_fetch_row(result);
sql = "update tb_user set NAME= '";
strcat(dest1, sql);
// printf("%s",sql);
bookname = (char*)malloc(20);
CString str1 = bbb;//,str1,str2;
LPCTSTR p2 = str1.GetBuffer(0);
str1.ReleaseBuffer();
bookname = new char[str1.GetLength() + 1];
strcpy_s(bookname, str1.GetLength() + 1, CT2CA(p2));
UpdateData(TRUE);
strcat(dest1, row[1]);
strcat(dest1, "', PASSWD= '");
strcat(dest1, bookname); /*追加sql语句*/
strcat(dest1, "' where NUM= ");
strcat(dest1, ttt);
UpdateData(TRUE);
if (mysql_query(&mysql, dest1) != 0)
{
MessageBox(L"不能修改记录!", L"提示");
}
else
{
MessageBox(L"修改成功!", L"提示");
}
}
}
else
{
MessageBox(L"没有发现要修改的信息!", L"提示");
}
}
mysql_free_result(result); //释放结果集
}
mysql_close(&mysql); //释放连接
}
这是数据库的数据}
void mima::OnBnClickedOk()
{
MYSQL mysql; //
MYSQL_RES *result; //定义结果集变量
MYSQL_ROW row; //定义行变量
char ch[2];
//char id[10]; /*结果集中的行数*/
char *sql;
char dest[500] = { " " };
char dest1[500] = { " " };
char *bookname;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "localhost", "root", "556677", "db_book", 3306, NULL, 0))
{
MessageBox(L"连接数据库失败", L"提示");
}
else
{
char * ttt;
CString str = id1;//,str1,str2;
LPCTSTR p = str.GetBuffer(0);
str.ReleaseBuffer();
ttt = new char[str.GetLength() + 1];
strcpy_s(ttt, str.GetLength() + 1, CT2CA(p));
sql = "select * from tb_user where NUM=";
strcat(dest, sql);
strcat(dest, ttt); /*将图书编号追加到sql语句后面*/
if (mysql_query(&mysql, "select * from tb_user"))
{ //如果查询失败
MessageBox(L"查询 tb_user 数据表失败!", L"提示");
}
else
{
result = mysql_store_result(&mysql); //获得结果集
if (mysql_num_rows(result) != NULL)
{
strcpy(ch, "Y");
if (strcmp(ch, "Y") == 0) /*判断是否需要录入*/
{
row = mysql_fetch_row(result);
sql = "update tb_user set NAME= '";
strcat(dest1, sql);
// printf("%s",sql);
bookname = (char*)malloc(20);
CString str1 = bbb;//,str1,str2;
LPCTSTR p2 = str1.GetBuffer(0);
str1.ReleaseBuffer();
bookname = new char[str1.GetLength() + 1];
strcpy_s(bookname, str1.GetLength() + 1, CT2CA(p2));
UpdateData(TRUE);
strcat(dest1, row[1]);
strcat(dest1, "', PASSWD= '");
strcat(dest1, bookname); /*追加sql语句*/
strcat(dest1, "' where NUM= ");
strcat(dest1, ttt);
UpdateData(TRUE);
if (mysql_query(&mysql, dest1) != 0)
{
MessageBox(L"不能修改记录!", L"提示");
}
else
{
MessageBox(L"修改成功!", L"提示");
}
}
}
else
{
MessageBox(L"没有发现要修改的信息!", L"提示");
}
}
mysql_free_result(result); //释放结果集
}
mysql_close(&mysql); //释放连接
}
这是数据库的数据}