问题描述
在C ++中,我正在使用 mysql.h
库,我没有设法捕获MySQL错误(例如,由于主键中的冲突而无法插入)。
In C++, I am using mysql.h
libraries and I do not manage to catch MySQL errors (for example, failure to insert due to conflict in primary key).
我尝试过
#include <mysql.h>
// ...
try{
res = mysql_perform_query(conn, sqlIn);
} catch (...) {
// ...
}
但仍然不能避免中止:
MySQL query error : Duplicate entry
我正在使用PuTTy界面运行编译的c ++程序,并且程序中止,它会重现MySQL的错误(无论是否使用TRY CATCH or not)。
我没有发现任何关于MySQL使用catch语句的异常代码的引用。
I am running the compiled c++ program using PuTTy interface and as the program aborts, it reproduces MySQL's error (regardless of whether I use TRY CATCH or not).
I have not found any reference to specific exception codes for MySQL use with catch statement.
显然,C ++(使用这个库 mysql.h
)即使使用毯子也不会捕获MySQL回调错误syntax( catch(...)
)。由于此语法应该捕获所有异常,但在这种情况下,我不明白这里发生了什么。如何捕获SQL异常?
Apparently, C++ (using this library mysql.h
) does not catch MySQL callback errors even with the "blanket" syntax (catch (...)
). Since this syntax should catch ALL exceptions, yet doesn't in this case, I am at a loss to understand what is happening here. How can I catch the SQL exceptions?
推荐答案
mysql C库不会抛出任何的排除;它只是像大多数其他C库一样,在一个常见的地方(如errno)设置错误信息,并返回一个状态。您的客户端代码检查返回并抛出错误/异常。
The mysql C library does not throw any excptions; it merely, like most other C libraries, sets up error info in a common place ( like errno ) and returns a status. Its up to your client code to check the return and throw an error/exception.
如果您需要一个简单的解决方案来解决您的问题,请尝试使用mysql ++(AKA mysqlpp)。
这是一个我可以保证的软件;坚如磐石!
If you need an easy fix for your problem, try mysql++ (AKA mysqlpp).It is a piece of software that I can vouch for; solid as a rock!http://tangentsoft.net/mysql++/
这篇关于在c ++中捕获MySQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!