Page 1 of 1

Problem with unicode characters and SQLite (russian letters)

Posted: Sat May 05, 2012 3:17 pm
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

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 3:54 pm
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()

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 4:17 pm
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());

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 4:43 pm
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]) );

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 4:48 pm
by eriX
It didn't compile...

If I use this: WxListBox6->Append( wxString::FromUTF8(pazResult) );
I don't have any output in the ListBox.

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 5:20 pm
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

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 5:31 pm
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()

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 5:39 pm
by doublemax
Use a debugger and check the memory pazResult points to.

Re: Problem with unicode characters and SQLite (russian lett

Posted: Sat May 05, 2012 5:49 pm
by eriX
Thank you very much!
I got it! =D

- Eric