C++执行SQL语句UPDATE和DELETE失败,程序中断,但执行INSERT和SELECT却成功

用的VS2019和SQLsever2012

 

以下是代码

void Bookdata::select()
{
	CDatabase db;
	CRecordset rs(&db);
	CString varid;
	CString strdsn;
	CString sql;
	strdsn.Format("ODBC;DSN=sql;UID=sa;PWD=123456;DBQ=C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\bookdata.mdf");
	db.Open(NULL, false, false, strdsn, true);
	cout << "请输入需要查询书籍的ISBN号:";
	cin >> isbn;
	sql.Format("SELECT * from book where [ISBN]='%s' order by [ISBN]", isbn);
	rs.Open(CRecordset::forwardOnly, sql);
	if (rs.IsBOF() && rs.IsEOF())
	{
		cout << "没有找到此纪录!" << endl;
		cout << "请重新查询!" << endl;
		return select();
	}
	else
	{
		cout << std::left << "ISBN  号:"; 
		rs.GetFieldValue((short)0, varid); cout << std::left << varid<<endl;
		cout << std::left << "书    名:"; 
		rs.GetFieldValue((short)1, varid); cout << std::left << varid << endl;
		cout << std::left << "作    者:"; 
		rs.GetFieldValue((short)2, varid); cout << std::left << varid << endl;
		cout << std::left << "出 版 社:"; 
		rs.GetFieldValue((short)3, varid); cout << std::left << varid << endl;
		cout << std::left << "入库时间:"; 
		rs.GetFieldValue((short)4, varid); cout << std::left << varid << endl;
		cout << std::left << "数    量:"; 
		rs.GetFieldValue((short)5, varid); cout << std::left << varid << endl;
		cout << std::left << "批 发 价:"; 
		rs.GetFieldValue((short)6, varid); cout << setprecision(5)<< varid << endl;
		cout << std::left << "零 售 价:"; 
		rs.GetFieldValue((short)7, varid); cout << setprecision(5)<< varid << endl;
		cout << endl;
	}
	rs.Close();
	db.Close();
}

void Bookdata::addbook()
{
	CDatabase db;
	CRecordset rs(&db);
	CString strdsn;
	CString sql;
	strdsn.Format("ODBC;DSN=sql;UID=sa;PWD=123456;DBQ=C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\bookdata.mdf");
	db.Open(NULL, false, false, strdsn, true);
	cout << "正在执行书籍的增添!" << endl;
	cout << "*********************"<<endl;
	cout << "请输入要增添书籍的ISBN号:";
	cin >> isbn;
	sql.Format("SELECT * from book where [ISBN]='%s' order by [ISBN]", isbn);
	rs.Open(CRecordset::forwardOnly, sql);
	if (!rs.IsBOF() || !rs.IsEOF())
	{
		cout << "ISBN号重复,不可增添!"<<endl;
		rs.Close();
		return addbook();
	}
	cout << "请输入要增添书籍的书名:";
	cin >> bookTitle;
	cout << "请输入要增添书籍的作者:";
	cin >> author;
	cout << "请输入要增添书籍的出版社:";
	cin >> publisher;
	cout << "请输入要增添书籍的入库时间:";
	cin >> dateAdded;
	cout << "请输入要增添书籍的数量:";
	cin >> qtyOnHand;
	cout << "请输入要增添书籍的批发价:";
	cin >> wholesale;
	cout << "请输入要增添书籍的零售价:";
	cin >> retail;
	sql.Format("INSERT into book VALUES('%s','%s','%s','%s','%s','%d','%lf','%lf')",
		isbn, bookTitle, author, publisher, dateAdded, qtyOnHand, wholesale, retail);
	db.ExecuteSQL(sql);
	rs.Close();
	db.Close();
}

void Bookdata::updatebook()
{
	char newdata[20];
	int choice;
	CDatabase db;
	CRecordset rs(&db);
	CString strdsn;
	CString atb;
	CString sql;
	strdsn.Format("ODBC;DSN=sql;UID=sa;PWD=123456;DBQ=C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\bookdata.mdf");
	db.Open(NULL, false, false, strdsn, true);
	cout << "正在执行书籍的修改!" << endl;
	cout << "********************" << endl;
	cout << "请输入要修改书籍的ISBN号:";
	cin >> isbn;
	sql.Format("SELECT * from book where [ISBN]='%s' ", isbn);
	rs.Open(CRecordset::forwardOnly, sql);
	if (rs.IsBOF() && rs.IsEOF())
	{
		cout << "没有找到此ISBN号!" << endl;
		cout << "请重新查询!" << endl;
		return updatebook();
	}
	cout << "请选择要修改的属性:"<<endl;
	cout << "--------------" << endl;
	cout << "||1.ISBN号  ||" << endl;
	cout << "||2.书名    ||" << endl;
	cout << "||3.作者    ||" << endl;
	cout << "||4.出版社  ||" << endl;
	cout << "||5.入库时间||" << endl;
	cout << "||6.数量    ||" << endl;
	cout << "||7.批发价  ||" << endl;
	cout << "||8.零售价  ||" << endl;
	cout << "--------------"<<endl;
	cout << "请输入你的选择(1-8):";
	cin >> choice;
	switch (choice)
	{
		case 1:
			atb = _T("ISBN"); 
			break;
		case 2:
			atb = _T("book_name"); 
			break;
		case 3:
			atb = _T("author");
			break;
		case 4:
			atb = _T("publish"); 
			break;
		case 5:
			atb = _T("get_time"); 
			break;
		case 6:
			atb = _T("amount"); 
			break;
		case 7:
			atb = _T("trade"); 
			break;
		case 8:
			atb = _T("retail"); 
			break;
		default:cout << "错误!请输入(1-8)!"<<endl; return updatebook();
	}
	cout << "请输入修改后的数据:";
	cin >> newdata;
	sql.Format("UPDATE book set %s='%s' where [ISBN]='%s'",atb, newdata, isbn);
	db.ExecuteSQL(sql);
	rs.Close();
	db.Close();
}

void Bookdata::deletebook()
{
	CDatabase db;
	CRecordset rs(&db);
	CString strdsn;
	CString atb;
	CString sql;
	strdsn.Format("ODBC;DSN=sql;UID=sa;PWD=123456;DBQ=C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\bookdata.mdf");
	db.Open(NULL, false, false, strdsn, true);
	cout << "正在执行书籍的删除!" << endl;
	cout << "********************" << endl;
	cout << "请输入要删除书籍的ISBN号:";
	cin >> isbn;
	sql.Format("SELECT * from book where [ISBN]='%s' ", isbn);
	rs.Open(CRecordset::forwardOnly, sql);
	if (rs.IsBOF() && rs.IsEOF())
	{
		cout << "没有找到此ISBN号!" << endl;
		cout << "请重新查询!" << endl;
		return deletebook();
	}
	sql.Format("DELETE from book where[ISBN]='%s'", isbn);
	db.ExecuteSQL(sql);
	cout << "已成功删除!" << endl;
	rs.Close();
	db.Close();
}

是在一个类里实现的,然后用类对象调用UPDATE

int main()
{
	Bookdata a;
	a.updatebook();
}

然后执行UPDATE 报错,程序中断

求大佬相助

为什么我这样调用insert 和select都可以

而delete和update不行,是我类成员函数调用出错还是什么问题?

sql.Format("UPDATE book set %s='%s' where [ISBN]='%s'",atb, newdata, isbn);

这个SQL语句不对吧。

把sql变量的值打印出来看看。

这是SQL表中的数据

 


int main()
{
	Bookdata a;
	a.select();
}


​