简体   繁体   English

使用 R 中的 DBI 包附加到 SQL Server 数据库时,字符列被转换为数字

[英]Character columns being converted to numeric when appending to SQL Server Database with DBI package in R

I have a database hosted using Microsoft SQL Server.我有一个使用 Microsoft SQL Server 托管的数据库。 It has a table project with columns project_id ( int identity ), project_code ( nvarchar not null ), and api_form ( nvarchar ).它有一个包含project_id ( int identity )、 project_code ( nvarchar not null ) 和api_form ( nvarchar ) 列的表project I have connected to this using the DBI package in R and ran the following code.我已经使用 R 中的DBI包连接到它并运行了以下代码。

dbAppendTable(conn, 'project', data.frame(
  project_code = c("ABC123", "DEF456"),
  api_form = c(NA, NA)
))

When I query the entire table however, I get the following output.但是,当我查询整个表时,我得到以下输出。

project_id  project_code    api_form
1   1   NULL
2   2   NULL

I am not sure why the characters are now integers.我不确定为什么字符现在是整数。 Is there a way to add character data directly to the table or, if this is expected behaviour, how do I query the table to find the actually text I entered?有没有办法将字符数据直接添加到表格中,或者,如果这是预期的行为,我如何查询表格以找到我输入的实际文本?

Classic overlook of data.frame() call that casts strings as factors by default.默认情况下将字符串转换为因子的data.frame()调用的经典忽略。 Your project_code renders as factor (ie, numeric columns with label values) and not characters.您的project_code呈现为因子(即带有标签值的数字列)而不是字符。 To resolve, simply pass stringsAsFactors=FALSE , so actual character values are appended to database table:要解决,只需传递stringsAsFactors=FALSE ,这样实际的字符值就会附加到数据库表中:

dbAppendTable(conn, 'project', 
              data.frame(
                 project_code = c("ABC123", "DEF456"),
                 api_form = c(NA, NA),
                 stringsAsFactors=FALSE 
              )
)

Alternatively, set the argument as global option for all data.frame() calls in your R session:或者,将参数设置为 R 会话中所有data.frame()调用的全局选项:

options(stringsAsFactors = FALSE)

dbAppendTable(conn, 'project', 
              data.frame(
                 project_code = c("ABC123", "DEF456"),
                 api_form = c(NA, NA) 
              )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 中的 Copy_to 导致日期被转换为数字列 - Copy_to in R results in dates being converted to numeric columns 当添加的列较少时,追加到现有的SQLite表,而无需将数据库读入R - Appending to existing SQLite table when addition has fewer columns, without reading database into R 带有数字列的 SQL Server 数据透视表 - SQL Server Pivot-table with Numeric Columns SQL Server:将字符串转换为数字-从字符串转换日期和/或时间时转换失败 - SQL Server : convert string to numeric - Conversion failed when converting date and/or time from character string 使用 RSQLite 和 DBI 包的 sql 关键字 IN - sql keyword IN using RSQLite and DBI package 当我在数据库名称中包含数字字符时,SQL Server查询创建数据库给我一个错误 - SQL Server query to create database is giving me an error when I include numeric characters in the database name 使用R,ODBC和DBI的数值超出范围 - Numeric value out of range using R, ODBC and DBI SQL 将字符转换为数字 - SQL Convert character to numeric 使用DBI使用Perl连接到SQL Server吗? - Connecting to SQL Server using Perl using DBI? C#Express Edition + SQL Server 2008 R2 Express; 正在运行时重新创建的数据库文件 - C# Express Edition + SQL Server 2008 R2 Express; Database file being recreated on run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM