windows store apps開發使用SQLite-net 查詢語句中文亂碼問題

查詢英文又會出問題,正在解決中已解決

情況是這樣:1.表名含有中文,或者查詢條件含中文時,查詢時會返回Unrecognized token +中文亂碼

                        2.使用parameter時,不會將?替換

搜索了很久,大多數是教你怎麼添加sqlite runtime for win8 插件,怎麼添加sqlite-net驅動,千篇一律,中文的和老外的差不多

然後參考安卓對付亂碼的方法,把字符串轉爲utf-8,還是不行,感覺本來字符就是utf-8的。

仔細思考,首先肯定不是查詢語句的utf-8問題,其次不會是sqlite的問題,因爲數據庫是我在另一程序中用System.Data.Sqlite生成的,含中文表名,在SQLiteSpy中顯示正常。

考慮可能是1.sqlite runtime for win8不支持中文 2.sqlite-net驅動問題

到了今天早上,不甘心又仔細搜索,終於找到了這篇帖子http://bbs.csdn.net/topics/390254870,找到了解決辦法得意,看來還是我知識太淺,還要多加學習

下面把方法貼出來

在 sqlite.cs 類中

添加

 [DllImport("sqlite3", EntryPoint = "sqlite3_prepare16_v2", CallingConvention = CallingConvention.Cdecl)]
        public static extern Result Prepare16_2(IntPtr db, [MarshalAs(UnmanagedType.LPWStr)] string sql, int numBytes, out IntPtr stmt, IntPtr pzTail);



把默認的

public static IntPtr Prepare2(IntPtr db, string query)
        {
            IntPtr stmt;
            var r = Prepare_2(db, query, query.Length, out stmt, IntPtr.Zero);
            if (r != Result.OK)
            {
                throw SQLiteException.New(r, GetErrmsg(db));
            }
            return stmt;
        }


改爲
        public static IntPtr Prepare2(IntPtr db, string query)
        {
            IntPtr stmt;
            int num = System.Text.Encoding.Unicode.GetByteCount(query);
            var r = Prepare16_2(db, query, num, out stmt, IntPtr.Zero);
            if (r != Result.OK)
            {
                throw SQLiteException.New(r, GetErrmsg(db));
            }
            return stmt;
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章