Linux Program Write Oralce the database is displayed in Chinese as a question mark???
1. Introduction of the problem
The root cause is the character set problem, which is the character set of the database and the writing program of the Linux system is inconsistent with the character set;
But with export nls_lang= "Simplified Chinese" _china. ZHS16GBK Add the environment variable, or add the environment variable in the. bash_profile file, or add the environment variable in the/etc/profile file, make it effective with the source command, and write the Chinese or question mark in the Oralce database? , then has restarted the system, restarted the server, let the environment variable take effect, or display question mark;
2. correct method for setting character sets in code
(1) Viewing the type of character set on the server side
With command: Select Userenv (' language ') from dual View the character set type of the database is simplified Chinese_china. ZHS16GBK, note that the Chinese end must be ZHS16GBK;
(2) Set the environment variable with the following code before initializing the database in the program written to the database
Char chvaluename[] = "Nls_lang";
String strlang= "simplified Chinese_china. ZHS16GBK ";//must be consistent with the database server
int errorcode=setenv(Chvaluename, Strlang.c_str (), 1);
if (errorcode!=0)
{
ERROR ("Linux setenv%s failed errorcode%d!", Strlang.c_str (), errorcode);
}
Else
{
INFO ("Linux setenv%s succeed!", Strlang.c_str ());
}
(3) Compile the program, re-run, you can normally write Chinese characters to the Oracle database;
3. Character Set Introduction
Nls_lang format:
Nls_lang = Language_territory.charset
There are three components (language, geography, and character set), each of which controls the characteristics of the NLS subset. of which: language
Specifies the language of the server message.
territory specifies the date and number format for the server.
CHARSET specifies the character set, as long as this one consistent, you can write to the database, the value of this field determines the character conversion format, if not consistent, will not find the character, a Chinese character will be displayed as two question marks;
4.linux several ways to add environment variables
(1) Directly in the terminal with a command to add, the environment variable settings only in the Terminal window, the exit window will be invalid;
Export Nls_lang=simplified Chinese_china. Zhs16gbk
(2) added in the. bash_profile file,/etc/profile takes effect for all users, ~/.bash_profile only for the current user. Add with the command VI Bash_profile also with export nls_lang=simplified Chinese_china. Zhs16gbk
(3) added in/etc/profile, valid for all users, after modification, you need to use the source command to make it effective;
Vi/etc/profile
(4) Adding environment variables using shell scripts
If GREP-FXQ "Export nls_lang=\" simplified chinese\ "_china. ZHS16GBK "/etc/profile
Then
echo "Export nls_lang=simplified Chinese_china. ZHS16GBK found "
Else
echo "Add nls_lang=simplified Chinese_china. ZHS16GBK to File "
Sed-i ' $a export nls_lang=\ "simplified chinese\" _china. ZHS16GBK '/etc/profile
Source/etc/profile
Fi
5.windows set environment variables under
Char chvaluename[] = "Nls_lang";
String Strlang=simplified Chinese_china. ZHS16GBK;
HKEY HKEY = NULL;
DWORD Dwdatalen = Small_len;
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "System\\currentcontrolset\\control\\session Manager\\environment", 0, KEY_ Query_value, &hkey)! = ERROR_SUCCESS)
{
Db_debug ("RegOpenKey%s fail, err:%ld", "System\\currentcontrolset\\control\\session manager\\environment", GetLastError ());
Break
}
See if there are any environment variables first
if (RegQueryValueEx (HKey, chvaluename, NULL, NULL, (byte*) chdata, &dwdatalen) = = ERROR_SUCCESS)
{//If it is already set and the same is returned
if (Strlang.compare (chdata) = = HPR_OK)
{
RegCloseKey (HKey);
iRetVal = HPR_OK;
Break
}
}
RegCloseKey (HKey);
If not set or different, reset;
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, G_chregenvpath, 0, Key_set_value, &hkey)! = ERROR_SUCCESS)
{
Break
}
RegSetValueEx (HKey, Chvaluename, 0, REG_SZ, (const byte*) strlang.c_str (), strlang.length ());
Dword_ptr dwresult = 0;
Make immediate effect
LRESULT Lret = SendMessageTimeout (hwnd_broadcast, Wm_settingchange, 0, LPARAM ("Environment"), Smto_abortifhung, 2000, & Amp;dwresult);
if (Lret! = 0)
{
RegCloseKey (HKey);
Db_debug ("Change Oracle nls lang:%s to:%s success!", Chdata, Strlang.c_str ());
Break
}
Db_debug ("Change Oracle nls lang:%s to:%s failed!", Chdata, Strlang.c_str ());
RegCloseKey (HKey);
Linux program written to oralce database Chinese display as question mark??? Code implementation SET Environment variables!