我们正在使用 node-mssql 包插入和读取我们的 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
由于某种原因,当我将值插入“value”时,一些特殊字符没有得到很好的处理。
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'要插入的文本'。
它会起作用;)
关于sql-server - mssql特殊字符编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66045099/