Unicode字符串进入SQLite数据库 [英] Unicode strings into a SQLite database

查看:99
本文介绍了Unicode字符串进入SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将Visual Studio C#代码插入SQLite数据库中时,我希望获得一些帮助.

I would like some help with my Visual Studio C# code with inserting unicode strings into a SQLite database.

下面是我的测试代码,用于将测试字符串写入数据库:

Below is my test code to write a test string to the database:

         string testStr = "á Á ñ ç é á";

         SQLiteConnection mydataConnection = new SQLiteConnection();  // setup new sql connection obj
         try
         {
             ////                    SQLite DB
             mydataConnection.ConnectionString =
             "Data Source=C:\\Users\\John\\Desktop\\location.db; Version=3; UseUTF16Encoding=True;Synchronous=Normal;New=False"; // set up the connection string

             mydataConnection.Open();  // open the connection to the db

             SQLiteCommand myCmd = new SQLiteCommand();   // create a new command object
             myCmd.Connection = mydataConnection;   // whats its connected to, see above connection string


             SQLiteParameterCollection myParameters = myCmd.Parameters; // good pratice to use parameters to pass data to db
             myParameters.AddWithValue("@name", testStr);  //
             myCmd.CommandText = "INSERT INTO location (name) VALUES (@name)";
             myCmd.ExecuteNonQuery();
         }
         catch (SQLiteException d)
         {
             string myerror = "Database Error" + d;
             MessageBox.Show(myerror);
         }
    finally  // good pratice to close db connection in a finally so exceptions dont leave open.
         {
             mydataConnection.Close();
         } 

当我查看数据库/表(使用SQLite Administrator)时,字符串如下所示:ÃÃñçéÃ

When I view the database/table (using SQLite Administrator) the string looks like this: á à ñ ç é á

作为测试,我可以复制&使用SQLite Administrator将字符串直接粘贴到数据库中,然后保存该字符串,随后可以对其进行查看.

As a test, I can copy & paste the string direct into the database using SQLite Administrator and the string is saved and can subsequently be viewed OK.

我尝试切换"UseUTF16Encoding = True;"对/错-没什么区别.

I have tried toggling "UseUTF16Encoding=True;" True / False - no difference.

任何想法我在做什么错了.

Any ideas what I'm doing wrong.

推荐答案

这个问题原来是我用来查看/检查db/data的SQLite Administrator应用程序的问题.似乎是这个应用程序在我的代码插入时无法正确显示字符.奇怪的是,如果您使用SQLite Administrator应用程序直接添加测试文本(通过将测试字符串复制并粘贴到表/字段中)将显示,保存并保存测试文本.随后查看确定.无论如何,现在都可以使用SourceForge SQLite数据库浏览器来检查我的代码是否正确写入,并且一切看起来都井井有条.

This problem turned out to be an issue with the SQLite Administrator app I was using to view/check the db/data. Seems it was this app that would not display the characters correctly when inserted by my code. Strangely though if you used the SQLite Administrator app to add the test text directly (by copy & pasting the test string into the table/field) it would display, save & subsequently view OK. Anyway now using SourceForge SQLite Database Browser to check my code writes correctly and all seems in order.

非常感谢那些花时间发表评论的人,希望这对其他人有帮助.

Many thanks for anyone who took the time to comment, hope this is of help to someone else.

这篇关于Unicode字符串进入SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆