当字符串在 R 中包含 UTF-8 字符时,DBI 会遇到查询问题
DBI get query problems when string contain UTF-8 characters in R
我正在使用以下函数将此查询发送到 MySQL 数据库:
loadDataBudget <- function(korisnik, razinaLabel) {
lapply( dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)
# Connect to the database
db <- dbConnect(MySQL(), dbname = databaseBudget, host = options()$mysql$host,
port = options()$mysql$port, user = options()$mysql$user,
password = options()$mysql$password, encoding = "utf8")
# Construct the fetching query
query <- sprintf("SELECT positions, x201501, x201502, x201503, x201504, x201505, x201506, x201507, x201508, x201509,
x201510, x201511, x201512 FROM %s WHERE naziv = '%s' AND razina = '%d'",
tableBudget,
korisnik,
razinaLabel)
# Submit the fetch query and disconnect
data <- dbGetQuery(db, query)
dbDisconnect(db)
data
}
如果字符参数 korisnik
包含非 UTF-8 字符,函数运行良好。但如果它包含克罗地亚语 UTF-8 字符,它 returns 为空 table。我在 MySQL 数据库中尝试了两种归类:utf8_general_ci 和 utf8_croatian_ci .
我也试过在查询之前将名称设置为 utf-8,但没有帮助。
文本被截断了,应该有带重音的克罗地亚字母?
- 要存储的字节未编码为utf8mb4。解决这个问题。
- 另外,检查读取时的连接是否为UTF-8。
如果这还不够,请参阅 "truncate" 和
中的调试提示
R
可能需要:
Tool -> Global Options -> Code -> Saving and put UTF-8
rs <- dbSendQuery(con, 'set character set "utf8"')
rs <- dbSendQuery(con, 'SET NAMES utf8')
我正在使用以下函数将此查询发送到 MySQL 数据库:
loadDataBudget <- function(korisnik, razinaLabel) {
lapply( dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)
# Connect to the database
db <- dbConnect(MySQL(), dbname = databaseBudget, host = options()$mysql$host,
port = options()$mysql$port, user = options()$mysql$user,
password = options()$mysql$password, encoding = "utf8")
# Construct the fetching query
query <- sprintf("SELECT positions, x201501, x201502, x201503, x201504, x201505, x201506, x201507, x201508, x201509,
x201510, x201511, x201512 FROM %s WHERE naziv = '%s' AND razina = '%d'",
tableBudget,
korisnik,
razinaLabel)
# Submit the fetch query and disconnect
data <- dbGetQuery(db, query)
dbDisconnect(db)
data
}
如果字符参数 korisnik
包含非 UTF-8 字符,函数运行良好。但如果它包含克罗地亚语 UTF-8 字符,它 returns 为空 table。我在 MySQL 数据库中尝试了两种归类:utf8_general_ci 和 utf8_croatian_ci .
我也试过在查询之前将名称设置为 utf-8,但没有帮助。
文本被截断了,应该有带重音的克罗地亚字母?
- 要存储的字节未编码为utf8mb4。解决这个问题。
- 另外,检查读取时的连接是否为UTF-8。
如果这还不够,请参阅 "truncate" 和
中的调试提示R
可能需要:
Tool -> Global Options -> Code -> Saving and put UTF-8
rs <- dbSendQuery(con, 'set character set "utf8"')
rs <- dbSendQuery(con, 'SET NAMES utf8')