Trying to set up a simple MongoDB database connection in Windows 7, using the C++ driver. I'm using Visual C++ compiler 19 for x86, 32-bit MongoDB 3.0.6, Boost 1_59_0, Mongo legacy 1.0.5 C++ driver.
Driver compiles OK using the command
scons --cpppath=d:\boost_1_59_0 --libpath=d:\boost_1_59_0\stage\lib --msvc-host-arch=x86 install
Program is
#include <cstdlib>
#include <iostream>
using namespace std;
#include <WinSock2.h>
#include <windows.h>
#include "mongo/client/dbclient.h"
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
Program compiles using
cl /EHsc /I"c:\mongo-cxx-driver-legacy-1.0.5\build\install\include" /I"d:\boost_1_59_0" /DSTATIC_LIBMONGOCLIENT mdb.cpp c:\mongo-cxx-driver-legacy-1.0.5\build\install\lib\libmongoclient-s.lib /link /LIBPATH:"D:\boost_1_59_0\stage\lib" ws2_32.lib
But when I run the program, get the error message
caught can't connect couldn't initialize connection to localhost, address is invalid
The server is running OK as I can access it through the shell, add records etc.
This is my first time programming MongoDB and I'm kind of stuck. Any suggestions?
OK, problem solved (thanks to stevepowell.ca/mongo-db-1.html). Here's the answer for anyone else who runs into this problem:
Windows needs to initialize the client before setting up the connection.
#include <cstdlib>
#include <iostream>
#include <WinSock2.h>
#include <windows.h>
#include <memory>
#include "mongo/client/dbclient.h"
using namespace mongo;
using namespace std;
void run() {
mongo::client::initialize(); // this line is new
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
I wish this had been in the tutorial!
Onwards and upwards.
Related
I have problem when I use csignal.
I use Visual Studio 2019
#include <Windows.h>
#include <csignal>
#include <iostream>
void signalHandler(int signum)
{
std::cout << "Interrupt signal (" << signum << ") received.\n";
exit(signum);
}
int main()
{
std::signal(SIGINT, signalHandler);
while (1)
{
std::cout << "Going to sleep...." << std::endl;
Sleep(1);
raise(0);
}
std::cout << "Hello World!\n";
return 0;
}
when raise called after I had:
I have ucrtbased.dll in:
C:\Windows\System32
I installed Windows SDK. I don't understand what is wrong?
You're raising signal 0 (raise(0);) which is probably an invalid signal value.
You should use the standard #defines (which may have compiler-specific values) for parameter (see spec).
I wonder why the following programme crashes. How to use awaitable not with boost::asio::async_write/async_read functions.
Let's see:
#include <iostream>
#include <boost/asio/io_context.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
using boost::asio::io_context;
using boost::asio::co_spawn;
using boost::asio::awaitable;
using boost::asio::detached;
awaitable<void> task() {
std::cout << "hello" << std::endl;
return awaitable<void>{};
}
int main() {
try {
boost::asio::io_context io_context(1);
co_spawn(io_context, task(), detached);
io_context.run();
} catch(const std::exception &ex) {
std::cerr << ex.what() << std::endl;
}
return 0;
}
This results in
hello
(Segmentation fault)
What is wrong about it?
UPD: self-solved.
I should just use co_return. Thus it must be the following:
awaitable<void> task() {
std::cout << "hello" << std::endl;
co_return;
}
digging into the sources of boost::asio::awaitable, I figured out that I just should make use of co_return keyword. Surprisingly, it is not shipped with boost. It is enabled either by -fcoroutines flag or -std=c++20. Unexpectedly. Having said that, it is solved.
I'm currently learning c++ from scratch, I've previously developed apps with C# using Visual Studio but I'm a total noob with C++, I'm trying to make a small console exe that releases and renews the ip while practicing using headers and differents .cpp files.
The issue is that when I run the local windows debugger from visual studio 2015 the code runs perfectly and does everything I'm trying to. But when I build and try to run the .exe file it goes into an infinite loop stating endlessly the output from the std::cout <<"Realizando ipconfig Release", I have localized the issue to when it tries to run the system("ipconfig /release"). Why does this happen? and How can I fix it?
This is the header
#pragma once
#ifndef HeaderIp
#define HeaderIp
int Release();
int Renew();
#endif // !HeaderIp
This is the release.cpp
#include <stdlib.h>
int Release()
{
if (system(nullptr)==0)
{
return 0;
}
else
{
system("ipconfig /release");
return 1;
}
}
This is the renew.cpp
#include <stdlib.h>;
int Renew()
{
if (system(nullptr)==0)
{
return 0;
}
else
{
system("ipconfig /renew");
return 1;
}
}
and finally this is the ipconfig.cpp
#include <iostream>
#include <stdlib.h>
#include "HeaderIp.h"
#include <stdio.h>
int main()
{
std::cout << "Realizando ipconfig Release" << std::endl;
int i = 0; //Bit de informacion de status de CPU
i = Release();
if (i == 0)
{
std::cout << "Error al liberar IP" << std::endl;
system("pause");
exit;
}
else
{
std::cout << "Ip Liberado correctamente" << std::endl;
}
i = Renew();
if (i == 0)
{
std::cout << "Error al renovar IP" << std::endl;
system("pause");
exit;
}
else
{
std::cout << "Ip renovado correctamente" << std::endl;
}
system("pause");
return 0;
}
It's unusual, and indeed generally discouraged to use system in C++. If you were to manage DHCP leases using IpReleaseAddress and IpRenewAddress, instead, you might find your problem disappears.
You can use std::cin.sync() instead of system("pause"), too.
exit; probably doesn't do what you want it to, as you're only referring to the function, you're not calling it, so... it'll do nothing.
The semicolon in #include <stdlib.h>; is an error, and you should be including <cstdlib> and <cstdio> rather than <stdlib.h> and <stdio.h>.
Another question on a Mysql connection failing:
I'm using the Poco library (1.5.2) and I would like to know why, when I try to open a MySQL connection, I got this message:
Connection attempt failed
Whereas, when I try a connection via console (mysql -u root -p ...), it works.
Maybe I forget an important step in the MySQL configuration ?
Here is my code :
#include <iostream>
#include <string>
#include <Poco/Data/MySQL/MySQLException.h>
#include <Poco/Data/MySQL/Connector.h>
#include <Poco/Data/SessionFactory.h>
using namespace std;
int main()
{
Poco::Data::MySQL::Connector::registerConnector();
try
{
string str = "host=localhost;user=root;password=mypassword;compress=true;auto-reconnect=true";
Poco::Data::Session test(Poco::Data::SessionFactory::instance().create(Poco::Data::MySQL::Connector::KEY, str ));
}
catch (Poco::Data::MySQL::ConnectionException& e)
{
cout << e.what() << endl;
return -1;
}
catch(Poco::Data::MySQL::StatementException& e)
{
cout << e.what() << endl;
return -1;
}
return 0;
}
Thank you !!
ok the problem was the "localhost" value for "host" doesn't work on my linux (I don't know why). For fixing the bug, I had to change my string to:
string str = "host=127.0.0.1;user=root;password=mypassword;compress=true;auto-reconnect=true";
I am new to Thrift.
I am trying to create a table ("sample") in Hbase using the following Thrift program on ubuntu 10.10 and can anyone tell me whether this is correct or not.
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <poll.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "Hbase.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift;
using namespace std;
int main()
{
boost::shared_ptr<TTransport> socket(new TSocket("localhost", 60010));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
HbaseClient client1(protocol);
try
{
transport->open();
string tableName("Sample");
vector<ColumnDescriptor> columns;
columns.push_back(ColumnDescriptor());
columns.back().name = "cf:";
columns.back().maxVersions = 10;
columns.push_back(ColumnDescriptor());
columns.back().name = "test";
try {
client1.createTable(tableName, columns);
} catch (const AlreadyExists &ae) {
std::cerr << "WARN: " << ae.message << std::endl;
}
}
catch (const TException &tx) {
std::cerr << "ERROR: " << tx.what() << std::endl;
}
return 0;
}
But i am getting the following exception at this place client1.createTable(tableName, columns);
ERROR: No more data to read.
Please help in resolving this.
Got it,
Need to start thrift server on hbase by .<hbaseinstallationpath>/bin/hbase thrift -threadpool start