SQLite Forum

Spanish characters in SQLITE
Login

Spanish characters in SQLITE

(1) By Olivier (Bracque) on 2020-11-03 15:14:26 [link] [source]

Hello,
I am trying to use the sqlite dll with c++ compiler.
My problem is that the Spanish characters are not correctly insert in tables and not correctly retrieve in the select statement.
If somebody can help me.
Thanks a lot,
Olivier
The connexion is done with the function  :

Rc = sqlite3_open_v2(DataBase, &ConnexioLocal,SQLITE_OPEN_READWRITE,NULL);

And the select  :

char **SelectDirecte(char *SelectText,unsigned int  *NbColumnes)
{
  const unsigned char * valChar;
  int Rc;
  int type;
  int colCount;
  UnicodeString Result;

  if (Columnes != NULL)
  {
	delete [] Columnes;
	Columnes = NULL;
  }

  Rc = sqlite3_prepare(ConnexioLocal,SelectText,-1,&stmt,NULL);
  if( Rc != SQLITE_OK )
  {
	swprintf(Msg, 100, L"%hs", sqlite3_errmsg(ConnexioLocal));
	MessageBox(AplicacioHandle,Msg,L"DataBase",MB_ICONERROR);
	return NULL;
  }

  Rc = sqlite3_step(stmt);

  if ((Rc != SQLITE_DONE && Rc != SQLITE_OK))
  {
	colCount = sqlite3_column_count(stmt);
	Columnes = new char*[colCount];
	*NbColumnes = colCount;
  }

  if (Rc != SQLITE_DONE && Rc != SQLITE_OK)
  {
	for (int colIndex = 0; colIndex < colCount; colIndex++)
	{
		type = sqlite3_column_type(stmt, colIndex);
		const char * columnName = sqlite3_column_name(stmt, colIndex);
		if (type == SQLITE_INTEGER * 100)
		{
			sprintf(valChar, "%f\0",sqlite3_column_int(stmt, colIndex));
			Columnes[colIndex] = new char[strlen(valChar)+1];
			strcpy(Columnes[colIndex],valChar);
		}
		else if (type == SQLITE_FLOAT)
		{
			sprintf(valChar, "%f\0",sqlite3_column_double(stmt, colIndex));
			Columnes[colIndex] = new char[strlen(valChar)+1];
			strcpy(Columnes[colIndex],valChar);
		}
		else if (type == SQLITE_TEXT || type == SQLITE_INTEGER)
		{
			valChar = (const char*)(sqlite3_column_text16(stmt, colIndex));
			valChar = sqlite3_column_text(stmt, colIndex);
			Columnes[colIndex] = new char[strlen(valChar)+1];
			strcpy(Columnes[colIndex],valChar);
		}
		else if (type == SQLITE_BLOB)
		{
			printf("columnName = %s,BLOB", columnName);
		}
		else if (type == SQLITE_NULL)
		{
			printf("columnName = %s,NULL", columnName);
		}
	}
  }
  else
  {
	return NULL;
  }

  Rc = sqlite3_finalize(stmt);

  return(Columnes);
}

(2) By Clemens Ladisch (cladisch) on 2020-11-03 16:16:12 in reply to 1 [link] [source]

SQLite does not change the contents of strings (unless you tell it to do so).

How are you typing and display characters? The Windows console window does not really support UTF-8.

(3) By Olivier (Bracque) on 2020-11-03 16:32:33 in reply to 2 [link] [source]

Hello,
Thanks for your message. 
I do the display in a memo text.  I do a debug I see that the retrieve value is not correctly set.
If I do a 
valChar2 = static_cast<const wchar_t*>(sqlite3_column_text16(stmt, colIndex));

where valchar2 is a wchar_t pointer the value is correctly set. 
But I need to return a char * not a wchar_t .
thnaks,
Olivier

screen copy :

https://prnt.sc/vcpuig

(5.1) By Keith Medcalf (kmedcalf) on 2020-11-03 21:33:08 edited from 5.0 in reply to 3 [source]

Casts do not change any data. They merely cause the compiler to STFU and stop whining and complaining, or perhaps to whine and complain more.

That is to say that the result of:

(char*)sqlite3_column_text16(...)
is exactly and precisely the same as
(wchar_t*)sqlite3_column_text16(...)

It is just the "whining attribute" that has been changed.

Note for pendants: Yes, it is possible to change data via a cast however changing the "whine attributes" of a pointer does not change the data pointed to by the pointer or the pointer itself, the "whine attributes" of the pointer are all that are changed.

It is not possible to translate THE DATA from english to swahili using the following construct:

english *t = "English text";
(swahili*)t;

The "whine attribute" of the pointer is merely changed from english to swahili but the pointer itself and the data pointed at, is unchanged.

(6) By Clemens Ladisch (cladisch) on 2020-11-04 08:07:51 in reply to 3 [link] [source]

If you need the string as a char*, then use sqlite3_column_text() to begin with.

(4) By little-brother on 2020-11-03 20:05:13 in reply to 1 [link] [source]

I use UTF8 to store data in a sqlite database and convert it to UTF16LE before displaying in GUI. Also I convert user input from UTF16LE to UTF8 before push data into a database.

You could look at these function here (utf16to8 and utf8to16).

P.S. I use TCHAR-macro instead wchar_t.