SQL查询在Access中有效,但在R中使用RJDBC包时无效。

huangapple go评论72阅读模式
英文:

SQL Query works from Access but not from R using RJDBC package

问题

我整天都在尝试解决这个问题..
我有一个Access数据库,我想使用JDBC连接从R中更新一些数据。
如果我在Access中的SQL查询编辑器中执行它,UPDATE查询就能完美运行,但是在R中却不起作用,我不知道为什么。这是我的代码:

library(devtools)
library(DBI)
library(dplyr)
library(rJava)
library(RJDBC)

setwd("mypath")

access_driver_file_name <- "Access_JDBC42.jar"
access_driver_class <- "com.hxtt.sql.access.AccessDriver"
access_data_file <- "my_database.accdb"    #在这里放入数据库的名称
access_jdbc_url <- paste("jdbc:access:/", getwd(), "\\", access_data_file, sep="")
access_jdbc_driver <- JDBC(driverClass=access_driver_class, classPath=paste(getwd(), "\\", access_driver_file_name, sep=""))

con <- dbConnect(access_jdbc_driver, access_jdbc_url)
dbListTables(con)#这个完美运行

dbSendUpdate(con, 'UPDATE TableName SET TableName.Field = "Casa"
WHERE (([TableName].[Field]="Oficina"));')

如果我复制UPDATE TableName SET TableName.Field = "Casa" WHERE (([TableName].[Field]="Oficina"));并在Access中粘贴它,查询就能运行,但是R返回:

Error in .local(conn, statement, ...) : 
  execute JDBC update query failed in dbSendUpdate
  JDBC ERROR: Syntax error:  Stopped parse at .(id21)
  Statement: UPDATE TableName SET TableName.Field = "Casa"
WHERE (([TableName].[Field]="Oficina"));

我尝试过使用dbSendQuery和dbGetQuery,但总是得到相同的错误。
有人能帮帮我吗?
先谢谢了!

英文:

I have been all day trying to solve this problem..
I have an Access Database and I want to UPDATE some data from R using JDBC connection.
The UPDATE query works perfectly if I execute it from SQL Query editor in Access, but is not working from R and I dont know why. This is my code:

library(devtools)
library(DBI)
library(dplyr)
library(rJava)
library(RJDBC)

setwd(&quot;mypath&quot;)

access_driver_file_name &lt;- &quot;Access_JDBC42.jar&quot;
access_driver_class &lt;- &quot;com.hxtt.sql.access.AccessDriver&quot;
access_data_file &lt;- &quot;my_database.accdb&quot;    #poner aqui el nombre de la base 1
access_jdbc_url &lt;- paste(&quot;jdbc:access:/&quot;, getwd(), &quot;\\&quot;, access_data_file, sep=&quot;&quot;) 
access_jdbc_driver &lt;- JDBC(driverClass=access_driver_class, classPath=paste(getwd(), &quot;\\&quot;, access_driver_file_name, sep=&quot;&quot;))

con &lt;- dbConnect(access_jdbc_driver, access_jdbc_url)
dbListTables(con)#this work perfectly

dbSendUpdate(con, &#39;UPDATE TableName SET TableName.Field = &quot;Casa&quot;
WHERE (([TableName].[Field]=&quot;Oficina&quot;));&#39;)

If I copy UPDATE TableName SET TableName.Field = &quot;Casa&quot;
WHERE (([TableName].[Field]=&quot;Oficina&quot;)); and paste it in Access the query works, but R returns:

Error in .local(conn, statement, ...) : 
  execute JDBC update query failed in dbSendUpdate
  JDBC ERROR: Syntax error:  Stopped parse at .(id21)
  Statement: UPDATE TableName SET TableName.Field = &quot;Casa&quot;
WHERE (([TableName].[Field]=&quot;Oficina&quot;));

I tried the same with dbSendQuery and with dbGetQuery and I get always the same error.
Can anybody help me?
Thank you in advance!

I tried the same with dbSendQuery and with dbGetQuery and I get always the same error.

答案1

得分: 0

终于找到解决方案。
我更改了与Access连接的包。
现在我正在使用RODBC包。首先,我需要安装ODBC驱动程序到Access,然后在运行此代码并执行我的UPDATE查询时没有问题:

require(RODBC)
channel=odbcConnectAccess2007("my_path/my_data_base.accdb")
sqlQuery(channel, "UPDATE Table_Name SET Table_Name.column = 'Casa' WHERE (([Table_Name].[Column]='Oficina'));")
odbcClose(channel) #this is to desconect the DB
英文:

Finally I find the solution.
I change the package to connect with Access.
Now I using RODBC package. First I need to install ODBC driver to Access, and after I run this code and execute my UPDATE query without problem:

require(RODBC)
channel=odbcConnectAccess2007(&quot;my_path/my_data_base.accdb&quot;)
sqlQuery(channel, &quot;UPDATE Table_Name SET Table_Name.column = &#39;Casa&#39;
WHERE (([Table_Name].[Column]=&#39;Oficina&#39;));&quot;)
odbcClose(channel) #this is to desconect the DB

huangapple
  • 本文由 发表于 2023年3月7日 02:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定