wxSQLIte3, how to tie wxTextCtrl* and ExecuteUpdate()? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

wxSQLIte3, how to tie wxTextCtrl* and ExecuteUpdate()?

Post by Big_Lebowski »

Hello, I am newbie both in C++ and wxWidgets, wxSQLIte3.

Trying to read input from wxTextCtrl and push it into wxSQLite3 database.

I have this code:

Code: Select all

void PBGUI_MainFrame::OnAddClick( wxCommandEvent& event )
{
	wxString s_phone = m_textCtrlPhone->GetValue();
	wxString s_name = m_textCtrlName->GetValue();
	wxString s_town = m_textCtrlTown->GetValue();
	wxString s_address = m_textCtrlAddress->GetValue();

	extern wxSQLite3Database db;
	wxString  insertCmd("INSERT INTO phones VALUES (" + s_phone + ", " + s_name + ", " + s_town + ", " + s_address + ")");
	db.ExecuteUpdate(insertCmd);	
}

It seems to me, all should work OK, but it is not.
Something goes wrong with wxString....

I guess, it will be same trouble, when I'll be trying to read datas from wxSQLite3 database and push them into wxGrid...

What do I do wrong?

Thank you all.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

Something goes wrong with wxString....
i don't think many people here have psychic abilities.

With this kind of error description it'll be hard to help...
Use the source, Luke!
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Post by Big_Lebowski »

OK, Visual C++ 9 EE says:

Code: Select all


pbgui_mainframe.cpp
.\pbgui_mainframe.cpp(72) : error C2678: бинарный '+': не найден оператор, принимающий левый операнд типа 'const char [28]' (или приемлемое преобразование отсутствует)
        C:\wxWidgets-2.8.10\include\wx/string.h(1045): может быть 'wxString operator +(const wxString &,const wxString &)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1048): или       'wxString operator +(const wxString &,wxChar)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1050): или       'wxString operator +(wxChar,const wxString &)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1052): или       'wxString operator +(const wxString &,const wxChar *)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1055): или       'wxString operator +(const wxChar *,const wxString &)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1618): или       'wxString operator +(const wxString &,const wxWCharBuffer &)'
        C:\wxWidgets-2.8.10\include\wx/string.h(1620): или       'wxString operator +(const wxWCharBuffer &,const wxString &)'
        C:\wxWidgets-2.8.10\include\wx/arrstr.h(231): или       'wxArrayString::reverse_iterator::itor operator +(int,const wxArrayString::reverse_iterator::itor &)'
        C:\wxWidgets-2.8.10\include\wx/arrstr.h(232): или       'wxArrayString::reverse_iterator::itor operator +(const wxArrayString::reverse_iterator::itor &,int)'
        C:\wxWidgets-2.8.10\include\wx/arrstr.h(257): или       'wxArrayString::const_reverse_iterator::itor operator +(int,const wxArrayString::const_reverse_iterator::itor &)'
        C:\wxWidgets-2.8.10\include\wx/arrstr.h(258): или       'wxArrayString::const_reverse_iterator::itor operator +(const wxArrayString::const_reverse_iterator::itor &,int)'
        C:\wxWidgets-2.8.10\include\wx/longlong.h(1047): или       'wxLongLong operator +(long,const wxLongLong &)'
        C:\wxWidgets-2.8.10\include\wx/longlong.h(1060): или       'wxULongLong operator +(unsigned long,const wxULongLong &)'
        C:\wxWidgets-2.8.10\include\wx/string.h(1045): или       'wxString operator +(const wxString &,const wxString &)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1048): или       'wxString operator +(const wxString &,wxChar)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1050): или       'wxString operator +(wxChar,const wxString &)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1052): или       'wxString operator +(const wxString &,const wxChar *)' [найдено при поиске с зависимостью от аргументов]
        C:\wxWidgets-2.8.10\include\wx/string.h(1055): или       'wxString operator +(const wxChar *,const wxString &)' [найдено при поиске с зависимостью от аргументов]
        при попытке сопоставить список аргументов '(const char [28], wxString)'

User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

technically, "somestring" and wxString are not the same, so they can't be concatenated that easily.

Additionally, when creating SQL commands, certain chars need to be escaped, so they don't conflict with the SQL syntax.

So, it's better to use wxSQLite3StatementBuffer which handles this for you:

Code: Select all

wxSQLite3StatementBuffer bufSQL;
bufSQL.Format("INSERT INTO phones VALUES (%Q, %Q, %Q, %Q"), s_phone, s_name, s_town, s_address);
db.ExecuteUpdate(bufSQL);
There are some more alternatives to do this, check the "minimal" sample that comes with wxSQLite3
Use the source, Luke!
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Post by Big_Lebowski »

Thank you doublemax, but it looks like wxSQLite3StatementBuffer type is not wxString....

it reads first characters of the string...
For example:
phone: "5134782920" is written into databese as "5"
name: "Albert Einstein" is written into database as "A" and so on.....
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle »

Big_Lebowski wrote:but it looks like wxSQLite3StatementBuffer type is not wxString....
Right. This class is only a wrapper around the SQLite function sqlite3_vmprintf. It's use is restricted to what you can do with the C/C++ function printf.

I don't recommend using wxSQLite3StatementBuffer at all, since it makes your code vulnerable to SQL code injection.
Big_Lebowski wrote:it reads first characters of the string...
For example:
phone: "5134782920" is written into databese as "5"
name: "Albert Einstein" is written into database as "A" and so on.....
Not, if you use wxSQLite3StatementBuffer::Format correctly. But certainly passing wxString parameters directly to this method will produce unexpected results.

Take a look at the sample application accompanying wxSQLite3 and use prepared statements and method wxSQLite3Statement::Bind to pass values to your SQL statement.

Regards,

Ulrich
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Post by Big_Lebowski »

utelle wrote: Take a look at the sample application accompanying wxSQLite3 and use prepared statements and method wxSQLite3Statement::Bind to pass values to your SQL statement.

Regards,

Ulrich
Wow! it works :)) Thank you utelle, I can't believe it, I struggled with it for a week :)) I did this code:

Code: Select all

wxSQLite3Statement stmt = db.PrepareStatement("INSERT INTO phones VALUES (?, ?, ?, ?);");
    stmt.Bind(1, s_phone);
    stmt.Bind(2, s_name);
    stmt.Bind(3, s_town);
    stmt.Bind(4, s_address);
    stmt.ExecuteUpdate();
Now, I'll be trying to read datas from the database and push them into wxGrid, I guess it goes same way as wxSQLite3Statement.

Thanx utelle, doublemax once again.
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Post by Big_Lebowski »

Now, there are troubles with iserting datas into wxGrid...

I have this code:

Code: Select all



void PBGUI_MainFrame::OnQueryClick( wxCommandEvent& event )
{
		
	wxSQLite3ResultSet set = db.ExecuteQuery("SELECT * FROM phones");

	int s_col = 0;

	while (set.NextRow())
        {

	   wxString s_phone = set.GetAsString(_T("phone_id"));
	   wxString s_name= set.GetAsString(_T("name_id"));
	   wxString s_town = set.GetAsString(_T("town_id"));
	   wxString s_address = set.GetAsString(_T("address_id"));
	
	   m_grid1->InsertRows(0, 1);
	   m_grid1->SetCellValue(s_col, 0, s_phone);
	   m_grid1->SetCellValue(s_col, 1, s_name);
	   m_grid1->SetCellValue(s_col, 2, s_town);
	   m_grid1->SetCellValue(s_col, 3, s_address);

           s_col++;

        }

	 set.Finalize();

}

It works OK, excepting it inserts last row only, all rows before last one, in wxGrid control is empty, for example:
if phones table has 10 rows, the code inserts 9 empty rows, the last 10-th row (in the table) is inserted OK.

What do I do wrong?

How can I insert datas form databese into wxGrid control?

Thanx all.
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Post by Big_Lebowski »

The question is withdrawn, I just deleted "s_col++;" statement :) All works OK :)
Post Reply