Problem with unicode characters and SQLite (russian letters) 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
eriX
Experienced Solver
Experienced Solver
Posts: 84
Joined: Wed Feb 04, 2009 2:08 pm
Location: Germany
Contact:

Problem with unicode characters and SQLite (russian letters)

Post by eriX »

Hello,

actually I have got the following problem:

I enter some russian letters in a WxEdit field and want to save this in a SQLite Database - but the record doesn't become saved.
If I use normal characters everything works fine but if I use russian letters oder sth. like this nothing becomes written in my database.

Code: Select all

wxString command;
command << "REPLACE INTO vokabeln (listen_id, vokabel, uebersetzung) VALUES ('liste1', '"<< WxEdit1->GetValue() <<"', '"<< WxEdit2->GetValue() <<"');";

Code: Select all

sqlite3* Database;
		
	if(sqlite3_open(global_sprache_file_open, &Database) != SQLITE_OK)
	{
		wxMessageBox("ERROR", "ERROR", wxOK);
	}	
	else
	{
			int result = sqlite3_exec(Database, command.c_str() , 0 , 0 , 0);
								
			if (result!=SQLITE_OK)
			{
				wxMessageBox("could not execute sql command","error",wxOK);
			}
			else
			{
				wxMessageBox("command was successful executed","good",wxOK);
			}
	}

    sqlite3_close(Database);
My program says that the new record was written successful to the database but isn't in it.
If I add a record with an other database-management-tool it works perfectly.

Do you have any idea what I'm doing wrong?

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

Re: Problem with unicode characters and SQLite (russian lett

Post by doublemax »

First of all i'd recommend using wxSqlite3 which will handle all these character encoding issues for you:
http://forums.wxwidgets.org/viewtopic.php?t=33553

But if you want to use "raw" SQLite, try passing UTF8 encoded data to sqlite3_exec:

Code: Select all

int result = sqlite3_exec(Database, command.ToUTF8() , 0 , 0 , 0);
When you read the data back, you have to decode it again using wxString::FromUTF8()
Use the source, Luke!
eriX
Experienced Solver
Experienced Solver
Posts: 84
Joined: Wed Feb 04, 2009 2:08 pm
Location: Germany
Contact:

Re: Problem with unicode characters and SQLite (russian lett

Post by eriX »

Is there a devpack available for wxDevC++ ?

I can write now in the database correct with .ToUTF8()

But reading doesn't work with .FromUTF8()

Code: Select all

        char **pazResult=NULL;
        int pnRow;
        int pnColumn;
        char *pzpopup=NULL;
        
        sqlite3_get_table(Database, command, &pazResult, &pnRow, &pnColumn, &pzpopup);

        temp = pazResult[i];
        WxListBox6->Append(temp.FromUTF8());
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with unicode characters and SQLite (russian lett

Post by doublemax »

Code: Select all

WxListBox6->Append(temp.FromUTF8());
Does that event compile?

I don't know raw sqlite too well, but i guess it should look more like this:

Code: Select all

WxListBox6->Append( wxString::FromUTF8(pazResult[i]) );
Use the source, Luke!
eriX
Experienced Solver
Experienced Solver
Posts: 84
Joined: Wed Feb 04, 2009 2:08 pm
Location: Germany
Contact:

Re: Problem with unicode characters and SQLite (russian lett

Post by eriX »

It didn't compile...

If I use this: WxListBox6->Append( wxString::FromUTF8(pazResult) );
I don't have any output in the ListBox.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with unicode characters and SQLite (russian lett

Post by doublemax »

I don't know the context where this is called, does "i" have the right value?

See the the sqlite3_free_table documentation to see what the returned array pazResult contains:
http://www.sqlite.org/capi3ref.html#sqlite3_free_table
Use the source, Luke!
eriX
Experienced Solver
Experienced Solver
Posts: 84
Joined: Wed Feb 04, 2009 2:08 pm
Location: Germany
Contact:

Re: Problem with unicode characters and SQLite (russian lett

Post by eriX »

ok... "i" is a counter-variable in a for() loop...

I can only say that it worked (exept the russian lettes were displayed wrong) without: wxString::FromUTF8()
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with unicode characters and SQLite (russian lett

Post by doublemax »

Use a debugger and check the memory pazResult points to.
Use the source, Luke!
eriX
Experienced Solver
Experienced Solver
Posts: 84
Joined: Wed Feb 04, 2009 2:08 pm
Location: Germany
Contact:

Re: Problem with unicode characters and SQLite (russian lett

Post by eriX »

Thank you very much!
I got it! =D

- Eric
Post Reply