Mysql create procedure (create statement) inside qt - c++

I need a way to auto create a procedure inside my qt code. My seniors said that currently in our version of qt lib (4.7.3) it is not possible to use query.exec function. Maybe i can use qprocess? Please give me some ideas.

http://www.qtcentre.org/threads/16287-Cannot-create-MySQL-stored-procedure-views-from-Qt
This answer my question.
Do not use mysql delimiters (such as $$).
And separate the execution of drop and create.
QSqlQuery query;
QString command= "DROP PROCEDURE IF EXISTS PROC_X";
query.exec(command);
command = "CREATE PROCEDURE PROC_X";//(Just put your own statement there)
query.exec(command);

Related

Integrating SDelete into C++ Program

I am trying to securely clear out a directory using SDelete. I know that this is used from the Command line, but how I would I go about automatically clearing the directory from my C++ code, also using Qt if this has a built any built in functions. I could not find anything with searching and this is my first time doing something like this. Any help would be greatly appreciated, thanks.
It is good that you're not trying to re-create the functionality of SDelete. It would be a LOT of work to do as good as a job as what SDelete does. Invoking the existing application is a wise choice.
Now, on to your question... If you want to use QT, then what you need is something like this:
QString path = QString("sdelete", QStringList() << "Bogus.txt");
QProcess sdelete;
sdelete.start( path );
sdelete.waitForFinished();
That will start the process sdelete with the parameter Bogus.txt and then wait until the application is finished.
More Info: https://doc.qt.io/archives/qt-4.8/qprocess.html#start
Edit from OP : I found that using the following worked for me with the argument being passed in being a QString.
QProcess::execute("sdelete -s path");

Read window's registry in QT

I want to list all application which had been installed by reading uninstall registry file from HKEY_CURRENT_USER. But look like it can't be done by using QSettings, for some security reason ( i guess ).
QSettings maya("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall
People suggest to use WinAPI to accomplish this (at least, on Window platform)
Can somebody guide me how to add and use this lib please?
Thank
In order to get the list of all sub items under the "Uninstall" one in the Windows registry you need to use QSettings::childGroups() function, i.e:
QSettings m("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
QSettings::NativeFormat);
QStringList ak = m.childGroups();
This will return the list of all installed applications.
UPDATE:
After getting the list of installed applications one can read the installation details. There are two ways for doing that. For example to read the "UinstallPath" key for "Autodesk Maya 2014" application:
m.beginGroup("Autodesk Maya 2014");
QString path = m.value("UninstallPath").toString();
m.endGroup();
or simply:
QString path = m.value("Autodesk Maya 2014/UninstallPath").toString();

ODBC, SQL_SUCCESS_WITH_INFO, Cursor type changed - happening on a Stored Procedure with a single SELECT

I'm wondering whether anyone else has had this problem and how they resolved it?
Our application makes the follwoing ODBC call:
CString strCmd = "sprTestSingleSelect";
rc = SQLExecDirect(hstmt, (UCHAR*)(LPCSTR)strCmd, SQL_NTS);
The call retruns SQL_SUCCESS_WITH_INFO. By checking SQLGetDiagRec we see the message; Cursor type changed.
We found the following articles from Micrsoft;
http://support.microsoft.com/kb/156500/en-us
http://msdn.microsoft.com/library/ms130807.aspx
The stored procedure, sprTestSingleSelect, was created specifically to test what both articles hinted at - multiple selects cause the change.
CREATE PROCEDURE sprTestSingleSelect
AS
BEGIN
SET NOCOUNT ON;
SELECT id, firstname, lastname FROM address
END
GO
However, even with this very simple (single SELECT) stored procedure, the cursor type is still being changed (from SQL_CURSOR_KEYSET_DRIVEN to SQL_CURSOR_FORWARD_ONLY).
We need to cursor type to stay at SQL_CURSOR_KEYSET_DRIVEN, as later on in the application we are calling SQLFetchScroll(hstmt, SQL_FETCH_LAST, 0);, which is falling because of the incorrect cursor type.
Does anyone have an idea of what we might be doing wrong or what is going wrong?
We are using MS SQL Server 2008R2
Our application is written in C++ (using Visual Studio 10 Premium)
We've managed to get the above test working, however the solution is not very satisfactory.
When the store procedure only contains a single SELECT statement and nothing else it works. So to get it working all we had to do was remove the statement SET NOCOUNT ON;. As a side note, we tested setting NOCOUNT to OFF which also didn't work - removing the statement alltogether is what was required.
This seems to make using stored procedures to return data (result sets) rather useless!
If anyone has another (better) solution, we'd love to hear it...

How to use Qt/C++ to create/read/write files and store settings local with the program

I'm an unfortunate beginner at C++ and using the Qt GUI designer program seemed perfect for my needs, except I'm having problems trying to write out the code necessary for this. I could use the QSettings string to store local settings on the hard drive, but I personally hate it when programs do the %HOME_LOCAL%\APPS_SETTINGS bull that some do. I need to save a text file for both settings and a local\host database, within the program directory, to remember strings to read from later.
What is the line of code I need to make use of a local host text database or is there a better option? And how can I store that with the local program inside its directory?
You can use QSettings with any file, with constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 ).
To get the program directory, you can use QCoreApplication::applicationDirPath().
So, answer to your question, statement to put after creation of QApplication instance:
QSettings *settings = new QSettings(
QCoreApplication::applicationDirPath() + "/settings.ini",
QSettings::IniFormat,
qApp);
But, as noted in the comments under question, if you're making your program for general distribution, you should use the OS default. Examine all the constructors of QSettings to see what it can do. User does not often have write permission in the application directory. Note that you can also store settings to Windows registry with QSettings::NativeFormat.

C++, OLE, Excel Automation: EAccessviolation at 00000800

I am writing an background service application that has to automatically read data from Excel 2003 files. But no matter what I try, the method OlePropertyGet() always results in an EAccessViolation error while trying to read from address "00000800".
The error always occurs at the last line of this code snippet, and seems independent of what parameter the method receives:
Variant excel, workbooks;
try
{
excel = GetActiveOleObject("Excel.Application");
}
catch(...)
{
excel = CreateOleObject("Excel.Application");
}
workbooks = excel.OlePropertyGet("Workbooks");
I've done some extensive google search on this, but found nothing that's even remotely helpful, only this forum thread where someone has the same issue, but doesn't give any information about the cause or solution (it's somewhat funny that at one point the author mentions he knows the cause, but doesn't say what it is!).
I'm open to any ideas as to what is causing this and how to solve this problem, but also alternative approaches to Excel OLE automation.
My guess is its a null pointer issue..
It looks like neither GetActiveOleObject() nor CreateOleObject() worked.
Try checkign the validity of 'excel' before calling OlePropertyGet.
And I guess you should make sure you have Excel installed.
You can use Visual Studio Tools for Office (see http://msdn.microsoft.com/en-us/library/d2tx7z6d.aspx).
Or you can use ATL support to instantiate the object model provided by office.
Your code may not be able to resolve "Excel.Application" successfully, leading to a null pointer. It uses a registry lookup with that string to identify Excel. It sounds like you're missing that registry entry.
I use such code to determine validity of created objects(in C++ Builder):
Varaint excel = GetActiveOleObject("Excel.Application");
TAutoDriver<IDispatch> dispatcher;
dispatcher.Bind(excel, false);
if (dispatcher.IsBound())
{
Variant workbooks = excel.OlePropertyGet("Workbooks");
}