I am having a problem connecting to the database using MariaDB. When I try to do anything with conn, it prints a memory violation.
I use Linux Mint 20.1!!
#include <iostream>
#include <memory>
#include <mariadb/conncpp.hpp>
int main(int argc, char**argv)
{
sql::Driver* driver = sql::mariadb::get_driver_instance();
sql::SQLString url("///");
sql::SQLString base("///");
std::cout << driver->getName() << std::endl;
sql::Properties properties({
{"base", "base"},
{"password", "password"}
});
sql::Connection*conn = driver->connect(url,properties);
conn->setSchema(base);// here
}
Does anyone know what the problem is?
Thank you in advance for your help.
driver->connect may return nullptr. I guess that is happening. You don't check conn.
There is also DriverManager::getConnection, that unlike Driver::connect will throw an exception in such case.
Related
I want to write a C++ program to get associated applications which are suitable to open specified file. I find the LSCopyApplicationURLsForURL API, and create a command line C++ application by XCode.
But after running this program, I always get segment fault. XCode shows EXEC_BAD_ACCESS(code=1, address....) error.
I also tryied running it from sudo, but the same result. What is the problem?
The code:
#include <iostream>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
using namespace std;
int main(int argc, const char * argv[]) {
auto url = CFURLRef("file:///Users/efan/src/a.cpp");
auto ret = LSCopyApplicationURLsForURL(url, kLSRolesAll);
cout << ret << endl;
return 0;
}
Try creating your CFURLRef using one of the proper CFURLCreate* methods. See "Creating a CFURL" here.
For example:
auto tempStringURL = CFStringCreateWithCString(nullptr, "/Users/efan/src/a.cpp", kCFStringEncodingUTF8);
auto url = CFURLCreateWithFileSystemPath(nullptr, tempStringURL, kCFURLPOSIXPathStyle, FALSE);
auto ret = LSCopyApplicationURLsForURL(url, kLSRolesAll);
You need to Release the "Created" variables to clean up memory.
I want to call Microsoft Vision API from C++ and I am using cpr library to make requests. Now I'm run the following code:
#include <iostream>
#include <cpr/cpr.h>
#include <json.hpp>
int main(int argc, char** argv) {
auto response = cpr::Post(
cpr::Url{"https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze"},
cpr::Body{{"url","https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg"}},
cpr::Header{{"Ocp-Apim-Subscription-Key", "xxxxxxxx"}}
);
std::cout << response.status_code ;
auto json = nlohmann::json::parse(response.text);
std::cout << json.dump(4) << std::endl;
}
Though the code is running by cmake, so make was successful. But when I executed the executable, the following error appeared:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Aborted (core dumped)
PS: the documentation for Microsoft Vision API can be found here
So, tell me if I am doing some mistake. Also if someone know that how to send http requests in QtQuick app please tell me
Can you try this?
auto my_json = nlohmann::json::object({
{ "url","https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg" }
});
response = cpr::Post(
cpr::Url{"https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze"},
cpr::Body{ my_json.dump() },
cpr::Header{{"Ocp-Apim-Subscription-Key", "XXXXXX"}}
);
I want to use the mongo-cxx-driver to upload files but can't find a way. Tried to use the gridfs feature of mongodb but couldn't integrate. Using current stable version mongodb-cxx-driver (3.1.1).
gridFs throws error when try to store file like this:
gfs.storeFile("filepath", "filename");
Error: store_file: /usr/include/boost/smart_ptr/scoped_ptr.hpp:99: T* boost::scoped_ptr::operator->() const [with T = mongo::AtomicWord]: Assertion `px != 0' failed.
Aborted (core dumped)
Also if mongo client is initialized it provides segmentation fault error.
#include "mongo/client/dbclient.h"
#include <iostream>
#include <cstdlib>
using namespace std;
using namespace mongo;
int main(int argc, const char** argv) {
cout<<"good so far"<<endl;
client::GlobalInstance instance; //everytime producing segmentation fault
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Successfully initialized the client driver: " << instance.status() << std::endl;
}
return EXIT_SUCCESS;
}
That looks like the legacy client, not the stable 3.1.1 version.
GridFS is not yet available for the stable client (initial priority was on essential CRUD features), but GridFS is under active development and will be available in the 3.2.0 release expected in the next couple months. If you want to keep an eye on progress, the relevant JIRA ticket is CXX-1130.
I am trying to grab the names of webcams plugged into my computer and shove them into a combobox, then access the name later. Here is my code:
#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication app{ argc, argv };
QComboBox combo;
QList<QCameraInfo> info = QCameraInfo::availableCameras();
foreach(QCameraInfo i, info)
combo.addItem(i.description());
combo.show();
std::cout << combo.currentText().toStdString() << std::endl;
return app.exec();
}
The code creates and shows a combo box that has the name of a webcam that I have plugged in to the computer. It will then toss me an access violation exception in trying to print the combo box string to the console.
If I comment the cout line out, all is well, but on exit I get a Debug Assertion Failed! message:
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
which I take to mean I am deleting an object that has been deleted (the QString in the combobox???).
If I change the code to fill the combo box with dummies:
#include <QApplication>
#include <QComboBox>
#include <QCameraInfo>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication app{ argc, argv };
for(int i=0; i<2; i++)
combo.addItem(QString("la la la");
combo.show();
std::cout << combo.currentText().toStdString() << std::endl;
return app.exec();
}
I get the same error on the cout, but if I comment that line out, the application exits correctly. I am using Visual Studio 2013, Windows 7, and Qt5.
Now it works. I kept the same source code, but completely scrapped the existing project and started a new one from scratch.
I've discovered that if I set the Runtime Library flag to Multi-Threaded DLL Debug, I will get access violation errors. If I set it to Multi-Threaded DLL, it is fine.
There may have been some other project settings that contributed, but this seems to be the main culprit.
main.cpp
#include "sqlConnection.h"
#include <iostream>
int main() {
sqlConnection *sqlC = new sqlConnection();
sqlC->ifSucceed();
}
sqlConnection.h
#include <Windows.h>
#include <stdio.h>
#include <winsock.h>
#include "mysql.h"
#include <iostream>
#pragma once
using namespace std;
class sqlConnection
{
public:
sqlConnection();
~sqlConnection();
void ifSucceed();
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
};
sqlConnection.cpp
#include "sqlConnection.h"
sqlConnection::sqlConnection()
{
mysql_init(&mysql);
mysql_real_connect(&mysql, "xxxxx", "xxxx", "xxxx", "librarySys", 7726, NULL, 0);
}
void sqlConnection::ifSucceed() {
char *sql = "select * from tb_bookcase";
mysql_query(&mysql, sql);
result = mysql_store_result(&mysql);
if ((row = mysql_fetch_row(result)) != NULL) {
cout << "succeed!" << endl;
} else {
cout << "faiiiiiiiiled" << endl;
}
}
sqlConnection::~sqlConnection() {
}
IF there is a libmysql.dll in source file error the message : There
are untreated exception:0x00007FFED00441E6 (libmysql.dll) (in the
librarySys.exe ) 0xC0000005: Access conflict happened when reading
0x0000000000000010.
And then I have to stop. VS give me choice, to change PDB, the binary file path and retry. But I do not know how to do it.
If I delete the libmysql.dll , the error message is:
The program can not be started for losing libmysql.dll in the
computer.Try to reinstall this program to solve this problem.
It's so confusing! I have tried many ways to connect. There are always error messages.
All mysql_* functions return a value that indicates success or failure. Your code disregards them. You ought to confirm whether calls were successful before proceeding to the next call. I surmise you actually failed to connect to the DB and your MYSQL is remains invalid.
Use your debugger. There's no point in coding in the dark. With your debugger you will quickly see whether your MYSQL object has been properly initialized.
Another thing:
sqlConnection *sqlC = new sqlConnection();
There is no reason for this to be heap allocated.