can't connect to database by executable file - c++

i wrote a program that need to connect to database to insert some data , my executable file not connect to database but when i checked with code connection established! i don't know what is problem , do you know what is problem?
i use Qt to connect to database and my database is on mySql and here is how i connect :
soccer_db = QSqlDatabase::addDatabase("QMYSQL" , "sss");
soccer_db.setHostName(addrrFile.c_str());
soccer_db.setDatabaseName("sss");
soccer_db.open();
if (!soccer_db.open()){
emit dsignal("ssss not opened. Ckech whether server is down or change config file");
return false;
}

You are getting a failure because you try to open the database twice. The first attempt succeeds but the second one fails. Remove the first call to open, like this
soccer_db = QSqlDatabase::addDatabase("QMYSQL" , "sss");
soccer_db.setHostName(addrrFile.c_str());
soccer_db.setDatabaseName("sss");
if (!soccer_db.open()){
emit dsignal("ssss not opened. Ckech whether server is down or change config file");
return false;
}

i want to answer my question, in qt you should address your files completely not relative address, if you need to address completely you can give current directory by QtDir

Related

ODBC Connection via CDatabase fails with connection string from previous succesfull connection

I'm a bit clueless at the moment what the problem is with this code.
It trows a exception of type CDBException when trying to connect with the given ODBC Connect String and never comes back from .OpenEx - the lower message boxes never display.
There is a message from the ODBC Driver that states that the DSN isnt specified and that no default driver is found.
The Message Box that shows the connect string gives a good looking string - as it should, coming from a working connection
ODBC;DSN=DBTEST;Trusted_Connection=Yes;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
Same problem when using SA/Password for the connection
ODBC;DSN=DBTEST_PW;UID=SA;PWD=password;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
After reading other posts I added "DRIVER=" to the beginning and "SERVER=localhost;" to the end but that changed nothing. Resulting tested connection strings where
DRIVER=ODBC;DSN=DBTEST;Trusted_Connection=Yes;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
DRIVER=ODBC;DSN=DBTEST;Trusted_Connection=Yes;APP=XXX;WSID=NEUROMANCER;DATABASE=master;SERVER=localhost
DRIVER=ODBC;DSN=DBTEST_PW;UID=SA;PWD=password;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
DRIVER=ODBC;DSN=DBTEST_PW;UID=SA;PWD=password;APP=XXX;WSID=NEUROMANCER;DATABASE=master;SERVER=localhost
I also made sure to use the 32bit ODBC Admin tool to generate the system DSNs for the 32bit application.
If I replace "CDatabase::NoOdbcDialog" with "0" it opens the ODBC-Dialog again as if informations necessary to open the connection are missing.
db.OpenEx(NULL, CDatabase::forceOdbcDialog);
CString cstmp;
if (db.IsOpen()) {
cstmp = db.GetConnect();
db.Close();
}
MessageBox(cstmp, _T(""), MB_OK);
if (db.OpenEx(cstmp, CDatabase::noOdbcDialog )) { //Exception here CDBException at 0x004FDD9
MessageBox(_T("Connection success!"), _T("Connection"), MB_OK);
db.Close();
}
else {
MessageBox(_T("Connection failed!"), _T("Connection"), MB_OK);
}
Can anyone tell me where my problem is?

copying binary files to remote location in C++

I'm in the process of trying to copy an hdf5 binary file on a local machine to a remote computing blade. I am using libssh to copy the desired directory or files out after they are generated by my Qt application. Using libssh I am able to open an ssh_session, authenticate it, open a channel and send remote commands.
for (QStringList::iterator it = ipList.begin(); it != ipList.end(); ++it)
{
ssh_session my_session = new ssh_new();
QString ip_address = *it;
ssh_options_set(my_session, SSH_OPTIONS_HOST, ip_address.toStdString().c_str());
// Connect...Authenticate using public key....
QString command = QString("rm -r %2; cp -r %1 %1; cp /local/file.txt /remote/file.txt").arg(local_dir, remote_dir);
execute_remote_command(my_session, command.toStdString().c_str());
// Open channel and execute command
ssh_disconnect(my_session);
ssh_free(my_session);
}
This command is being executed for each individual computing blade. In between each of the calls I am closing and opening an ssh session to the next blade. The files make it out the blades but they appear to be corrupt. They are the exact same file size. I haven't figured out a way to compare the individual bytes to see just how corrupt they are, any tips there would be appreciated as well.
When I run my ssh copy commands in a separate test terminal program the files appear to make it intact and are readable on the blades. The issue only seems to occur when the files are moved from within the Qt GUI program.
EDIT: So delving a little bit deeper into what is wrong, it appears that the file on the remote server is not the same size. It appears to be missing a large portion of the bytes. On top of that when I examine what is there byte by byte with the local version of the file, almost all of the bytes differ.
Turns out the answer was that the HDF5 writer wasn't being closed properly before the SSH commands were being called. I fixed the problem by dynamically allocating the custom H5 class that someone else wrote and made sure to delete it before the SSH commands were called. Turns out whoever wrote the HDF5 read and write class didn't handle file opening and closing properly and didn't provide functions to do so.
Below is an example of what I am talking about.
HDF5writer_class *hdf5_writer = new HDF5writer_class();
hdf5_writer->create_file("/local/machine/hdf5_file.h5");
// ... add the data to the file
delete hdf5_writer;
// Open SSH Session and run the copy commands
Long story short, make sure the file you are writing is closed and released for use before you try to copy it.

Check status of file during sftp

I want to write a C++ code to get a file from server B via server A using password less sftp.The file on server B is infact being copied (via sftp ) from another server C. I was able to retreive the file from server B , however even if the file was still being copied, I was still able to get the file(incomplete file as it was still being transferred to server B from server C). I want to put a check if the file is being copied then i should not get it using sftp and wait till it it is completely moved. As far as i know sftp prompt does not support lot of commands. Can somebody please give me some inputs on how can i achieve this?
A traditional way to do this is to transfer the (big) "paydata" file, and a (small / empty) "flag" file after that. You (on the receiving end) wait until the flag file exists. If it does, the transfer of the paydata file has finished; delete the flag file, and do whatever you do with the paydata file.

C++ WinINet InternetReadFile function refresh

I am trying to get the content of a file using WinHTTP in C++. The file is a XML File and is generated by a executable on a server.
The code for init, connect and even read a file on the specified server address is working.
// Connect to internet.
m_hInternet = InternetOpen(L"HTTPRIP",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
// Check if worked.
if( !m_hInternet )
return;
// Connect to selected URL.
m_hUrl = InternetOpenUrlA(m_hInternet, strUrl.c_str(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RESYNCHRONIZE, 0);
// Check if worked.
if( !m_hUrl )
return;
if( InternetReadFile(m_hUrl, buf, BUFFER_SIZE, &bytesread) && bytesread != 0 )
{
// Put into std::string.
strData = std::string(buf,buf+bytesread);
}
Now I want to update the file (same address). The server update the file at 50Hz and I want my code to be able to ReadFile only if it has been updated by the server. Can InternetReadFile do that kind of thing? Maybe with a FLAG but I didn't find a thing on MSDN.
Thanks for your help.
There is no way in the HTTP protocol for you directly do that, hence there is no such function in WinHTTP. The easiest solution might be to download the file and see if it's changed, if the file is relatively small, or if the file is large, let the server which writes the file, also write a timestamp, checksum or counter increment file next to it.
Then your code would download the checksum file, see if it's changed, and in that case download the original file.
Or another solution would be to put a timestamp or similar data in the beginning of the XML file, and stop downloading the file if the timestamp (or checksum) is not updated. (This comes with its own drawbacks of course, you may have to write your own parser.)
If HTTP server has a page with info (e.g. timestamp) on this file (no matters that a file is generated; the page may be generated too), you may examine this page.
As you know that server updates the file with (nearly) constant speed, your app may just use the timer.
P.S. I doubt if there's really a sense in reading some file 50 times every second.

Why it connected continuously whether the path is correct or wrong

I've written the following code to connect with sqlite database file .
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("c://database.db");
if(!db.open())
ui->label->setText(ui->label->text() + "Failed to connect to database");
else
ui->label->setText(ui->label->text() + "Connected.");
When change the correct path another wrong path (ex: c://datafile.db or c:database.db), it remain prints Connected. text.
Notice: If the database file does not exists, or the correct path changed to another wrong path, it will create an empty database file into the wrong path.
Is there is any problem in my code to prints Connected all time?
If you are using SQLite then it will always attempt to create the database file if it can. This may not be what you want -- for example, if you need to use data from a pre-existing database. What you should do is verify that the file exists on disk before trying to open.
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QFile file ("c:/database.db");
if (file.exists()) {
if(!db.open()) {
ui->label->setText(ui->label->text() + "Failed to connect to database");
}
else {
ui->label->setText(ui->label->text() + "Connected.");
}
}
else {
ui->table->setText("Database does not exist!");
}
Note that the forward slash / works for any platform, and is less confusing than having to escape with the \ character. To have better portability you should probably replace the hard coded C:/ with something from QDir::root() or QDir::drives().
I can't spot it in the documentation, but some mailing list threads suggest that QSqlDatabase::open will always return true because it will create an empty database if there is no database at the given path. Note, as others have pointed out, that C://database.db is likely to be a valid path - most C++ libraries accept '/' as a path separator on Windows. It is also fairly common for libraries to convert paths you give them to 'canonical paths', which will usually involve cleaning up things like extra path separators and missing path separators after a drive name.