SOCI / soci

Official repository of the SOCI - The C++ Database Access Library
http://soci.sourceforge.net/
Boost Software License 1.0
1.41k stars 477 forks source link

gibberish error mysql windows #1029

Closed youngquan closed 1 year ago

youngquan commented 1 year ago

I am using soci with cmake and vcpkg. I have already installed soci[mysql]:windows-x64 from vcpkg.

Here are some parts from CMakeLists.txt:

find_package(SOCI CONFIG REQUIRED)
find_package(libmysql CONFIG REQUIRED)

target_link_libraries(main PRIVATE SOCI::soci_core SOCI::soci_empty SOCI::soci_mysql)
target_link_libraries(main PRIVATE libmysql)

Below is the code used for test:

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

using namespace soci;
using namespace std;

int main()
{

    try
    {
        session sql("mysql", "db=xxx user=xxx password=xxx");
        int count;
        sql << "select count(*) from software_settings", into(count);
    }
    catch (exception const& e)
    {
        cerr << "Error: " << e.what() << '\n';
    }
}

I got these garbled outputs:

Error: H��H�̉Hh�D$P��t�=l

Does anybody have a cue?

vadz commented 1 year ago

It could be mojibake although I have no idea how could you end up with a Unicode string in the exception object. The obvious suggestion is to try debugging this, i.e. put a breakpoint on the exception in the debugger and check what message it is constructed with.

youngquan commented 1 year ago

Thanks for your reply.

I tried to change the exception type:

    catch(soci::soci_error const& e)
    {
        cout << e.get_error_message() << std::endl;
        cout << e.get_error_category() << endl;
        cout << e.what() << std::endl;
        cerr << "Error: " << e.what() << '\n';
    }

Below are the outputs:

H��H�̉Hh�D$P��t�=l
7
H��H�̉Hh�D$P��t�=l
Error: H��H�̉Hh�D$P��t�=l

It seems the error category is unknown.

The excepiton seems to happen at when libmysql.dll is called. It is a runtime_error.

Here is a screenshot of the excption:

image

I also tried to specify the host and port in the connection string, however it still did not work.

vadz commented 1 year ago

You should be looking at the place the exception is thrown from, not where it's caught.

youngquan commented 1 year ago

I tried to build soci from source, and all the tests of mysql passed. Then I tried to run the code above, using soci from vcpkg, however, the gibberish error never occured again. I can not reproduce this bug any longer. I have no idea of what is going on. Maybe the experimental feature of windows that using Unicode UTF-8 for worldwide language support is unstable. Anyway, sorry for the late reply and the unreproducible error.