![](/img/trans.png)
[英]MSSQL - JPA - Character encoding for Special characters - appending 'N' nativeQuery
[英]Mssql encoding problem with special characters
我們正在使用 node-mssql package 插入和讀取我們的 azure mssql 數據庫。 數據庫非常簡單,因為它只是用作鍵/值緩存,其中值可以很長並且還包含特殊字符。
數據庫架構:
create table cache
(
id int identity
constraint cache_pk
primary key nonclustered,
cacheKey varchar(500),
ttl bigint,
value varchar(max)
)
go
create index cache_cacheKey_index
on cache (cacheKey desc)
go
create index cache_ttl_index
on cache (ttl)
go
出於某種原因,當我將值插入“值”時,一些特殊字符沒有得到很好的處理。
Dash – example
變成:
Dash example
我看到法國撇號也發生了同樣的事情。 我也嘗試過更改排序規則,但這並沒有幫助。 還嘗試使用nvarchar(max)
作為列類型。
這是插入代碼(包括 sql):
const result = await conn.request()
.input('key', mssql.VarChar, key)
.input('value', mssql.Text, value)
.input('ttl', mssql.BigInt, cacheEndTime)
.query`INSERT INTO cache (cacheKey, value, ttl) VALUES (@key, @value, @ttl)`;
您能否提供正確的表結構或 sql 語句來幫助完成這項工作?
我不確定是否可以幫助您,但是您檢查過表、數據庫和服務器的排序規則嗎? 校對有不同的級別。
您的問題的答案在以下一項中:
例如,如果您創建 nvarchar(如果您有國際場景,我建議您)字段,將文本插入轉換為 N'text to insert'。
它會起作用的;)
我找到了答案。 就像@RealSeik 和@Larnu 已經說過的那樣,這可能不是數據庫或查詢本身的問題,而是輸入問題。
我意識到, node-sass
有一個類型為 Unicode 文本,他們負責正確地轉換它。 因此,我將其更改為mssql.NText
mssql.Text
所以現在插入命令如下所示:
const result = await conn.request()
.input('key', mssql.VarChar, key)
.input('value', mssql.NText, value)
.input('ttl', mssql.BigInt, cacheEndTime)
.query`INSERT INTO cache (cacheKey, value, ttl) VALUES (@key, @value, @ttl)`;
我還為我的其他腳本添加了排序規則,這也是很好的衡量標准。 (僅此一項並沒有幫助,但可以很好地衡量)
ALTER DATABASE MyDbName
COLLATE Latin1_General_100_CI_AI_SC_UTF8 ;
create table cache
(
id int identity
constraint cache_pk
primary key nonclustered,
cacheKey varchar(500) COLLATE Latin1_General_100_CI_AI_SC_UTF8,
ttl bigint,
value varchar(max) COLLATE Latin1_General_100_CI_AI_SC_UTF8
)
go
create index cache_cacheKey_index
on cache (cacheKey desc)
go
create index cache_ttl_index
on cache (ttl)
go
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.