wxWidgets and wxDatabase

In this forum you can discuss database related issues which can be wxWidgets related, but also generic in nature.
Post Reply
Melandr
Experienced Solver
Experienced Solver
Posts: 53
Joined: Thu Oct 28, 2021 6:07 am

wxWidgets and wxDatabase

Post by Melandr »

Good day!
I am using wxWidgets 3.0.5 and the wxDatabase library. With the help of Mtangoo, I managed to build this library for ODBC. The library example compiles normally and runs when the library is built. I use CodeBlocks for development. Tell me how to properly set up your development environment to use the wxDatabase library. Since if you copy the source files of the example into a new project, I cannot figure out which dependencies need to be included in order for the project to compile normally.
Let's say I have a console application working with MS SQL server via ODBC API.
To build this program, I need to include the libodbc32 and libodbccp32 libraries. And include header files
#include <sql.h>
#include <sqlext.h>
#include <windows.h> //!first include
#include <sqltypes.h> //!

#include <stdio.h>
#include <tchar.h>
odbc.jpg
odbc.jpg (58.63 KiB) Viewed 9770 times
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 470
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: wxWidgets and wxDatabase

Post by doublemax@work »

If you successfully built the wxDatabase libs, in order to use them in a different project you have to:

- add the wxDatabase libs to the link libraries
- add the path to the wxDatabase libs to the linker settings (so that the linker can find them)
- add the path to the wxDatabase header files to the compiler settings (so that the compiler can find them)

As i don't use CodeBlocks, i can't tell you where exactly these settings are, but they shouldn't be too hard to find.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxWidgets and wxDatabase

Post by evstevemd »

Hi,
Did you try generating CodeBlocks Project and add to C::B?
Used CB years ago and have no Idea how to do it.
Alternatively you can do

Code: Select all

make install
and add the include and Libs to Code::Blocks project.

The most important thing is making sure you add reference to libs and include files in your Project no matter how you generated them
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Melandr
Experienced Solver
Experienced Solver
Posts: 53
Joined: Thu Oct 28, 2021 6:07 am

Re: wxWidgets and wxDatabase

Post by Melandr »

Good day!
I decided not to open a new topic, but to ask a question in this one.
Where can I read the wxDatabase documentation? Is it possible, when connecting to a database, to create a "Connection" object and store the connection string in it, so that it is possible, for example, to display the address of the database server, etc.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxWidgets and wxDatabase

Post by evstevemd »

Melandr wrote: Mon Nov 29, 2021 11:52 am Good day!
I decided not to open a new topic, but to ask a question in this one.
Where can I read the wxDatabase documentation?
Documentation is found on https://mtangoo.github.io/database/index.html
You can also generate it locally using Doxygen
Melandr wrote: Mon Nov 29, 2021 11:52 am Is it possible, when connecting to a database, to create a "Connection" object and store the connection string in it, so that it is possible, for example, to display the address of the database server, etc.
Well you can create your own class that contains wxDatabase object and metadata you want to store.

Another option is storing database information in wxConfig and use wxDatabase::GetDatabase(....) to create connection. Then you can read the same information from the same wxConfig object. I have never used that though, so I would be interested to hear feedback.

I always create my own connection class and add metadata, some of which are complex objects, in addition to wxDatabase. Ultimately, whatever works for you, go for it in this case!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Melandr
Experienced Solver
Experienced Solver
Posts: 53
Joined: Thu Oct 28, 2021 6:07 am

Re: wxWidgets and wxDatabase

Post by Melandr »

Good day! Do not tell me how to display a photo in the wxGrid cell correctly. The photo is stored in the database as a blob
After searching the forum, I found that I need to use wxMemoryBuffer, but I can't figure out how to display the image further. I do the data selection from the sql table as follows:

Code: Select all

wxDatabaseResultSet *pResults = m_Database->RunQueryWithResults(strSqlQuery);
...
if (pResults)
{
	while (pResults->Next())
	{
		wxMemoryBuffer data;
		void* pBlob = pResults->GetResultBlob(_("FIMG"),data);
		char* pRetrievedBuffer = (char*)pBlob;

		//Grid1->SetCellRenderer(countOfRows,0,pResults->GetResultBlob("FPHOTO")); ????
		Grid1->SetCellValue(countOfRows,1,wxString::Format(_("%i"),pResults->GetResultLong(_("FID"))));
		Grid1->SetCellValue(countOfRows,2,wxString::Format(_("%s"),pResults->GetResultString(_("FLAST"))));
		Grid1->SetCellValue(countOfRows,3,wxString::Format(_("%s"),pResults->GetResultString(_("FFIRST"))));
		Grid1->SetCellValue(countOfRows,4,wxString::Format(_("%s"),pResults->GetResultString(_("FMIDDLE"))));
		Grid1->SetCellValue(countOfRows,5,wxString::Format(_("%s"),pResults->GetResultString(_("FDEPT"))));
		Grid1->SetCellValue(countOfRows,6,wxString::Format(_("%s"),pResults->GetResultString(_("FTITLE"))));
		countOfRows++;
		if(countOfRows >= Grid1->GetNumberRows())
			Grid1->AppendRows();
	}
	m_Database->CloseResultSet(pResults);
}
m_Database->Close();
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxWidgets and wxDatabase

Post by evstevemd »

Hi Melandr,
Sorry that I could not reply b'se the forum won't let me.
Melandr wrote: Fri Dec 17, 2021 6:59 am Good day! Do not tell me how to display a photo in the wxGrid cell correctly. The photo is stored in the database as a blob
After searching the forum, I found that I need to use wxMemoryBuffer, but I can't figure out how to display the image further.
Can you post how you are inserting your data? I mean how do you convert image from file up until saving to the database?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Melandr
Experienced Solver
Experienced Solver
Posts: 53
Joined: Thu Oct 28, 2021 6:07 am

Re: wxWidgets and wxDatabase

Post by Melandr »

Good day! Only now I was able to continue working with the application I Explain. I have a database where one of the fields is a bmp photo saved as a blob. On request, a pointer to the result of the request is returned. How to display the image correctly in the application. I do not save the image in the database, but only display it.
Below is a selection from the database
1.jpg
I found two examples of working with images in, but there the image is output from the file, it is not clear to me how to output it from binary data.
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets and wxDatabase

Post by doublemax »

So this is not really database related, it's only about drawing a bitmap in a wxGrid cell? In that case, check this thread:
viewtopic.php?p=163315&sid=f4f13cc0b918 ... 6e#p163315
However, for this the row height needs to be as high as the image. Maybe it's better to have a separate area for the image which gets updated when a row gets focused.

Regarding creating a wxImage from a file in memory:

Code: Select all

wxImage* LoadImageFromBlob(const unsigned char *data, int size)
{
  if( data != NULL )
  {
    wxMemoryInputStream mi(data, size);
    wxImage *img = new wxImage(mi, wxBITMAP_TYPE_ANY);
    if( img != NULL && img->IsOk() ) return img;
    // wxLogDebug( wxT("DB::LoadImageFromBlob error: data=%p size=%d"), data, size);
    // caller is responsible for deleting the pointer
    delete img;
  }
  return NULL;
}
Use the source, Luke!
Melandr
Experienced Solver
Experienced Solver
Posts: 53
Joined: Thu Oct 28, 2021 6:07 am

Re: wxWidgets and wxDatabase

Post by Melandr »

Thanks for your help.
About a separate area for a photo. I am translating working software from Delphi to C++. And the images are displayed in each row as thumbnails. And when you double-click on a line, a new form opens with extended information and an enlarged photo. Therefore, it is possible that in order to display a photo in a wxGrid cell, it must be compressed to an acceptable size for display in the cell.
PS:I would also like to ask a question about wxSmith. Let's say I made a layout for the location of controls on a form using the wxSmith visual editor. But later I had a need to add some elements to the form and for this I should also use wxBoxSizer. The question is as follows. If I add a sizer not as a resource, but by adding code, how do I correctly update the resources so that it appears on the Resourse tab? Or suppose I have a source code with no resources, is it possible to get a resource file based on the code and then work on the project using wxSmith ?
Below is a screenshot.
I changed the location of panel 1 in the code, but the old values ​​remained in the properties of the same panel. Highlighted with red rectangles. Is it possible to update resources when changing the code manually?
2022-01-13_164818.jpg
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxWidgets and wxDatabase

Post by doublemax »

Sorry, i can't help with wxSmith related issues. And it would be better to open a new thread for issues that are unrelated to your initial question. In this case under "Compiler / Linking / IDE Related" category.
Use the source, Luke!
Melandr
Experienced Solver
Experienced Solver
Posts: 53
Joined: Thu Oct 28, 2021 6:07 am

Re: wxWidgets and wxDatabase

Post by Melandr »

Good day! Thanks for your help, it turned out to display the image by converting the binary data. Now I'm figuring out how to display the resulting image in a cell with a wxGrid grid. In principle, our similar topics are on the forum, you need to create a new wxBmpGridCellRenderer class based on the wxGridCellRenderer class, overriding the Draw method in it. But since I'm still not very familiar with the concept of object-oriented programming, the question arose of how to properly pass an image to the wxGridCellRenderer class.
Let me explain in more detail:
- method wxImage* testWxSmithFrame::LoadImageFromBlob(const unsigned char *data, int size) returns an image in wxImage format.
- next I get my own cell renderer from wxGridCellRenderer - class wxBmpGridCellRenderer
- further, when rendering the wxGrid grid, I assign a new renderer SetCellRenderer(row, col, new wxBmpCellRenderer(bmp));
- a bmp object, wxBitmap type, is passed to the wxBmpCellRenderer constructor.
The question is, which class would it be correct to make this image a member of? Maybe it would be more correct to include the LoadImageFromBlob method in the wxBmpGridCellRenderer class? Sorry for possibly stupid questions, I'm just learning.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2408
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: wxWidgets and wxDatabase

Post by evstevemd »

The screenshot you shared have Grid horizontally laid out with Image. That could be wxGrid + wxSizer + wxPanel
But since this is not wxDatabase related open new thread to avoid polluting this one!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply