如何在 R 中的 MySQL 中通过查询插入整数值?
问题描述:
我遇到了需要使用 R 编写的查询在表中插入整数值的情况.例如:
I come to the situation where I need to insert the integer values in the table with query which is written in R. for Example:
>n1<-20
>n2<-30
>library(DBI)
>library(RMySQL)
>drv<-dbDriver("MySQL")
>con<-dnConnect(drv,user="root",password="sam123",dbname="user")
>dbSendQuery(con,"insert into test values(n1,n2);") # Problem with this lines only
出现以下错误:
mysqlExecStatement(conn, statement, ...) 中的错误:RS-DBI 驱动程序:(无法运行语句:字段列表"中的未知列n1")
Error in mysqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not run statement: Unknown column 'n1' in 'field list')
请帮我解决这些问题
答
尝试粘贴功能进行拼接
dbSendQuery(con,paste("insert into test values(",n1,",",n2,");",sep=""));