I am trying to work on the project which is to connect MariaDB with C++ application. I referred to the URL: https://mariadb.com/docs/clients/connector-cpp/#installing-mariadb-connector-c-via-msi-windows. This URL is quite a good source to connect MariaDB with C++. However, it doesn't describe how to deal with the lib file and dll file. When I installed the MariaDB connector/C++ via MSI, it gave me several files: conncpp.hpp, mariadbcpp.dll, mariadbcpp.lib, etc.
I tried to include mariadb/conncpp.hpp by setting the path C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include and I did same with the lib file. Also, I went to the properties and set the linkers for the lib file. Here is the code that I am planning to execute:
// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>
// Main Process
int main(int argc, char** argv)
{
try
{
// Instantiate Driver
sql::Driver* driver = sql::mariadb::get_driver_instance();
// Configure Connection
// The URL or TCP connection string format is
// ``jdbc:mariadb://host:port/database``.
sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");
// Use a properties map for the user name and password
sql::Properties properties({
{"user", "db_user"},
{"password", "db_user_password"}
});
// Establish Connection
// Use a smart pointer for extra safety
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
// Use Connection
// ...
// Close Connection
conn->close();
}
// Catch Exceptions
catch (sql::SQLException& e)
{
std::cerr << "Error Connecting to MariaDB Platform: "
<< e.what() << std::endl;
// Exit (Failed)
return 1;
}
// Exit (Success)
return 0;
}
But whenever I compile and execute the code, it says Unhandled exception(0x00007FF918058D25(mariadbcpp.dll), MariaDB_Connection.exe): 0xC0000005:Access violation reading location 0xFFFFFFFFFFFFFFFF at the line of
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
.
Could you tell me how to solve this problem?
You've hit the deficiency in the C++ connector API. Or call it a bug. It's Windows specific. Debug and release versions of STL objects may have different layout. Switching build to release configuration should help. But some issues are still possible depending on VS version used. As a workaround you could also try to use other connect method, i.e. std::unique_ptr<sql::Connection> conn(driver->connect(url, "db_user", "db_user_password")); since it doesn't rely on Properties map, that causes the issue here.
User contributions licensed under CC BY-SA 3.0