3

This is my code.

#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;
    }

}

In fact, my database student doesn't have the table student1, so I think it will catch an exception due to that. But what I thought is wrong. This is what it outputs:

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

Obviously, It doesn't catch the exception. What's wrong with it? And my makefile is this:

 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:
underscore_d
  • 6,309
  • 3
  • 38
  • 64
lxc
  • 67
  • 9
  • 6
    Are you using a version of the library that is affected by [this problem](https://sourceforge.net/p/soci/mailman/message/26451676/), in which basically exceptions are thrown from _destructors_? – Mr.C64 Oct 18 '17 at 09:00
  • 1
    I know it a bit off topic, but soci is very buggy and is not well maintained, I experienced some odd crashes when running for some hours, which seemed to be caused by improper connection handling of soci. So I recommend switching to another library. – Superlokkus Feb 12 '19 at 15:03

0 Answers0