0

I am trying to insert Persian string like "سلام" to SQL server 12 database with Poco ODBC. But in the database I see Characters like this "ط³ظ„ط§ظ…". the column data type is varchar(I try it with nvarchar too) and I test it with different Collation like Arabic_CI_AS and Persian_100_CI_AS.

there is no problem with data stored in the database. it is what I inserted into the database.

but when I try to see my database with Microsoft SQL Server Management Studio and another application with Qt interface, both of them show me "ط³ظ„ط§ظ…".

Does anyone have any idea how to fix it?

std::string updated = "سلام";
Statement select(session);
select << "INSERT INTO Employee VALUES( ?)",
    use(updated),now;
Nader
  • 318
  • 1
  • 6
  • 16
  • Check the value (in hex) of the original and stored data matches. This can be data problem or a display problem. – Richard Critten Jul 11 '19 at 13:01
  • 1
    The column MUST be nvarchar. But how are you inserting this to the database? Are you using parameters? Are those parameters datatypes defined as nvarchar? Or are you vulnerable to sql injection and munging strings together? – Sean Lange Jul 11 '19 at 13:01
  • *Don't* use `varchar`. Use `nvarchar` instead. Make sure you use parameterized queries and pass the text as Unicode strings too. StackOverlfow is written in C# whose strings are Unicode (UTF16), uses SQL Server and stores text in `nvarchar` fields. The data is passed as values in parameterized queries. As you see, it has no problem displaying Persian characters – Panagiotis Kanavos Jul 11 '19 at 13:01
  • @PanagiotisKanavos I tried with both of them – Nader Jul 11 '19 at 13:02
  • @RichardCritten the value is correct but how I can fix the display? – Nader Jul 11 '19 at 13:03
  • @nader tried *what* with both of them? Your own question proves `nvarchar` works. If you got garbled text you'll have to find out *where* the text was converted to ASCII. Did you use string concatenation instead of parameters? Did you use `std::string` instead of `u16string` ? Did you convert the string to ASCII before displaying it? – Panagiotis Kanavos Jul 11 '19 at 13:05
  • @PanagiotisKanavos I used parameters and std::string and I didn't use any conversion. – Nader Jul 11 '19 at 13:07
  • Without some code posted about how you are inserting and/or displaying this data it is a guessing game. – Sean Lange Jul 11 '19 at 13:07
  • @SeanLange I posted some code. – Nader Jul 11 '19 at 13:13
  • 1
    `std::string` is ASCII so the text was *already* converted to ASCII. – Panagiotis Kanavos Jul 11 '19 at 13:14
  • Try `auto updated = u"سلام";` to get a `const char16_t*` string or `auto updated = u8"سلام";` to get a UTF8 encoded `const char*` and make sure you save the source file as UTF8 or UTF16 – Panagiotis Kanavos Jul 11 '19 at 13:18
  • You can also use `auto updated = u"سلام"s;` to get a `u16string`. ``auto updated = u8"سلام"s;` will create a UTF8 encoded `string` – Panagiotis Kanavos Jul 11 '19 at 13:19
  • @PanagiotisKanavos before using SQL server I used SQLite and I didn't have any problem with std string. – Nader Jul 11 '19 at 13:21
  • @nader it's easy to prove that nvarchar has no issue. Just create a table or table variable with an `nvarchar(50)` field, insert the value you want *as a Unicode literal* and then retrieve it, eg `declare @t table (n nvarchar(50)); insert into @t values (N'سلام') select * from @t`. The text will come back the same – Panagiotis Kanavos Jul 11 '19 at 13:29
  • @PanagiotisKanavos you are right. there is no problem with my data. – Nader Jul 11 '19 at 13:46

1 Answers1

0

Please change 'سلام' to N'سلام'

RainyTears
  • 171
  • 4