为什么我无法捕获soci :: mysql_soci_error异常?

时间:2017-10-18 08:53:47

标签: c++ mysql database c++11 soci

这是我的代码。

#include <soci/soci.h>
#include<soci/mysql/soci-mysql.h>
#include <soci/postgresql/soci-postgresql.h>
#include<iostream>
#include<istream>
#include<ostream>
#include<string>
#include<exception>
#include<unistd.h>

using namespace std;
using namespace soci;

int main() {
    unsigned id=0;
    string name("lxc_lxc");
    int score=100;
    try {
        session sql("mysql://host=127.0.0.1 dbname=student user=root password=123");//建立连接
        transaction tr(sql);
        sql<<"insert into student1 (id, name, score) values (:item_0,:item_1, :item_2)", use(id,"item_0"),use(name,"item_1"), use(score + 1, "item_2");
        sql<<"insert into student (id, name, score) values (:item_0,:item_1, :item_2)", use(id,"item_0"),use(name,"item_1"), use(score + 2, "item_2");
        cout<<"before commit::"<<endl;
        usleep(1000000);
        tr.commit();
        cout<<"has commit"<<endl;
        sql.close();
    } catch (soci::mysql_soci_error const & e) {
        cout<<"mysql error::"<<e.what()<<endl;
    } catch (exception const & e) {
        cerr<<"Error::"<<e.what()<<endl;
    }

}

事实上,我的数据库学生没有表student1,所以我认为它会因此而捕获异常。但我认为是错的。这是它输出的内容:

terminate called after throwing an instance of 'soci::mysql_soci_error'
  what():  Table 'student.student1' doesn't exist while executing "insert into student1 (id, name, score) values (:item_0,:item_1, :item_2)" with :item_0=0, :item_1="lxc_lxc", :item_2=101.
[1]    21034 abort (core dumped)  ./main

显然,它没有捕获异常。它有什么问题?我的makefile是这样的:

 all: main.cpp
       g++ -std=c++11 -Wall -g -o main main.cpp  -lpq -lsoci_core -lsoci_postgr    esql -lmysqlclient -ldl -lpthread -I/usr/include/mysql
   clean:

0 个答案:

没有答案