在C ++中捕获MySQL错误 [英] Catch MySQL error in c++
问题描述
mysql.h
库,我不能捕获MySQL错误(例如,由于主键冲突导致无法插入)。 我尝试过
#include< mysql.h>
// ...
try {
res = mysql_perform_query(conn,sqlIn);
} catch(...){
// ...
}
但仍然不能避免中止:
MySQL查询错误:重复条目
我使用PuTTy接口运行编译的c ++程序,并且程序中止,它重现MySQL的错误(无论我是否使用TRY CATCH或不)。
我没有找到任何关于MySQL使用catch语句的特定异常代码的引用。
显然,C ++(使用此库 mysql.h
)不会捕获MySQL回调错误,即使 syntax( catch(...)
)。因为这种语法应该捕获所有的异常,但在这种情况下,我不知道这里发生了什么。如何捕获SQL异常?
mysql C库不会抛出任何限制;它只是像大多数其他C库一样,在一个常见的地方(如errno)设置错误信息并返回一个状态。
如果你需要一个简单的修复你的问题,请尝试mysql ++(AKA mysqlpp)。你的客户端代码检查返回并抛出错误/异常。
这是一个我可以保证的软件;坚如磐石!
http://tangentsoft.net/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).
I have tried
#include <mysql.h>
// ...
try{
res = mysql_perform_query(conn, sqlIn);
} catch (...) {
// ...
}
but it still does not avoid aborting with:
MySQL query error : Duplicate entry
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.
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?
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.
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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!