使用R变量将查询发送到RMYSQL的表格
我正在尝试编写我在R中创建的数据,并存储在MySQL表中的数组中。目前,这就是我的做法,
for(z in 1:60){
dbSendQuery(
mydb,
"insert into minutely_predictions(id,server_pred, server_hi, server_lo, os_pred, os_hi, os_lo, sessions_id) values(server__id,server_pred[i],server_hi[i],server_lo[i],os_pred[i],os_hi[i],os_lo[i],sesh_id)"
)
}
我会遇到MySQL语法错误,是否有一种方法可以将我试图从R传递到MySQL的变量?
谢谢。
I am trying to write data I have created in R and stored in arrays to MySQL table. Currently this is how I am doing it,
for(z in 1:60){
dbSendQuery(
mydb,
"insert into minutely_predictions(id,server_pred, server_hi, server_lo, os_pred, os_hi, os_lo, sessions_id) values(server__id,server_pred[i],server_hi[i],server_lo[i],os_pred[i],os_hi[i],os_lo[i],sesh_id)"
)
}
I'm getting MySQL syntax errors, is there a way to pass the variables I am trying to pass from R into MySQL?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑参数化的最佳实践,并避免变量与SQL语句的串联串联或插值。
注意:上面只有使用首选
rmariaiadb
(替换遗产rmysql
)。Consider the best practice of parameterization and avoid string concatenation or interpolation of variables to SQL statements.
Note: above may only work if using the preferred
RMariaDB
(replacement of legacyRMySQL
).我能够通过使用函数sprintf并以这种方式存储变量来修复它,希望这可以对其他人有所帮助。
I was able to fix it by using the function sprintf and storing the variable that way, hopefully this may help others in the future.