3

I'm creating c# wpf application and I'm using SQLite database for my data (sqlite3.dll library). I'm from Slovenia so i need to store strings that contains characters like ščćžđ. I can succesfully store my data and set my dataGrid itemsSource when my query looks like this:

List<Odgovor> list = AppData.DBObject.Query<Odgovor>("select * from Odgovor");
            ObservableCollection<Odgovor> odgovorList = new ObservableCollection<Odgovor>(list);
            this.dataGridOdgovor.ItemsSource = odgovorList;

It's succesfull when i do query

"select * from Odgovor where PersonName = \"Nick\"";

But i don't get any resoults when i do this query (and data are in my database so it's SQLite encoding problem).

"select * from Odgovor where PersonName = \"Saša\"";

I wasn't able to find solution so far. I can't find how sqlite replace special characters so that i could replace every special character in code... but i believe that there is prettier solution :)

klonitklonit
  • 319
  • 4
  • 12

1 Answers1

1

1) Try using parameters instead of literals. Database connectors should ensure proper character encoding.

See this link: Adding parameters in SQLite with C#

2) You can also try the encoding thechnique shown here:

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c1d98352-25a5-4ec4-8637-339fcee3715c/

Community
  • 1
  • 1
Yván Ecarri
  • 1,661
  • 18
  • 39
  • Can you please explain first method? – klonitklonit Sep 27 '12 at 11:01
  • Sure. I edited the post to show a link to another post where you can see how to pass parameters to SQLite in c# – Yván Ecarri Sep 27 '12 at 11:11
  • Second solution is not working `String personName = SkupniAtributi.personName;` `UTF8Encoding utf8 = new UTF8Encoding();` `byte[] b = utf8.GetBytes(personName);` `string str1 = utf8.GetString(b);` `tabOdgovor.Query = "select * from Odgovor where PersonName=\"" + str1 + "\"";` – klonitklonit Sep 27 '12 at 11:28
  • I'm sorry to see you decided to discard using parameters. Concatenating values in the query string is proven to a threat for applications security. – Yván Ecarri Sep 27 '12 at 12:30
  • I'm discarding parameter method because I don't have `SQLiteParameter` class. I'm using SQLite3.dll and SQLite.cs and SQLiteAsync.cs from Krueger Systems. For now I have soved my problem very badly (replacing č with #c and so on...) – klonitklonit Sep 28 '12 at 15:28