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

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

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

问题

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

  1. library(devtools)
  2. library(DBI)
  3. library(dplyr)
  4. library(rJava)
  5. library(RJDBC)
  6. setwd("mypath")
  7. access_driver_file_name <- "Access_JDBC42.jar"
  8. access_driver_class <- "com.hxtt.sql.access.AccessDriver"
  9. access_data_file <- "my_database.accdb" #在这里放入数据库的名称
  10. access_jdbc_url <- paste("jdbc:access:/", getwd(), "\\", access_data_file, sep="")
  11. access_jdbc_driver <- JDBC(driverClass=access_driver_class, classPath=paste(getwd(), "\\", access_driver_file_name, sep=""))
  12. con <- dbConnect(access_jdbc_driver, access_jdbc_url)
  13. dbListTables(con)#这个完美运行
  14. dbSendUpdate(con, 'UPDATE TableName SET TableName.Field = "Casa"
  15. WHERE (([TableName].[Field]="Oficina"));')

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

  1. Error in .local(conn, statement, ...) :
  2. execute JDBC update query failed in dbSendUpdate
  3. JDBC ERROR: Syntax error: Stopped parse at .(id21)
  4. Statement: UPDATE TableName SET TableName.Field = "Casa"
  5. 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:

  1. library(devtools)
  2. library(DBI)
  3. library(dplyr)
  4. library(rJava)
  5. library(RJDBC)
  6. setwd(&quot;mypath&quot;)
  7. access_driver_file_name &lt;- &quot;Access_JDBC42.jar&quot;
  8. access_driver_class &lt;- &quot;com.hxtt.sql.access.AccessDriver&quot;
  9. access_data_file &lt;- &quot;my_database.accdb&quot; #poner aqui el nombre de la base 1
  10. access_jdbc_url &lt;- paste(&quot;jdbc:access:/&quot;, getwd(), &quot;\\&quot;, access_data_file, sep=&quot;&quot;)
  11. access_jdbc_driver &lt;- JDBC(driverClass=access_driver_class, classPath=paste(getwd(), &quot;\\&quot;, access_driver_file_name, sep=&quot;&quot;))
  12. con &lt;- dbConnect(access_jdbc_driver, access_jdbc_url)
  13. dbListTables(con)#this work perfectly
  14. dbSendUpdate(con, &#39;UPDATE TableName SET TableName.Field = &quot;Casa&quot;
  15. 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:

  1. Error in .local(conn, statement, ...) :
  2. execute JDBC update query failed in dbSendUpdate
  3. JDBC ERROR: Syntax error: Stopped parse at .(id21)
  4. Statement: UPDATE TableName SET TableName.Field = &quot;Casa&quot;
  5. 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查询时没有问题:

  1. require(RODBC)
  2. channel=odbcConnectAccess2007("my_path/my_data_base.accdb")
  3. sqlQuery(channel, "UPDATE Table_Name SET Table_Name.column = 'Casa' WHERE (([Table_Name].[Column]='Oficina'));")
  4. 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:

  1. require(RODBC)
  2. channel=odbcConnectAccess2007(&quot;my_path/my_data_base.accdb&quot;)
  3. sqlQuery(channel, &quot;UPDATE Table_Name SET Table_Name.column = &#39;Casa&#39;
  4. WHERE (([Table_Name].[Column]=&#39;Oficina&#39;));&quot;)
  5. 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:

确定