简体   繁体   中英

R ODBC SQL Query with a filepath name (Eg for INSERT INTO … IN …)

Using R 2.13.0 with Window XP 32b,

I am struglling with defining properly a query that I'd like to build in R and send to sqlQuery from the RODBC package.

I have a problem with adding a filepath to the query.

following advices on how to deal with backslash in strings, here is the query that I can write but that lead to an error.

The following is running well in Access :

    SELECT Tamis_Lavage.* 
    FROM  Tamis_Lavage
    IN  "d:\Mes Documents\Pascal\03 - BiomFix\99 - Suivi Etude\01 - Donnees\Données STH\Test_Import\Copie de 20110623Acquisition.mdb"

is working fine in access.

its translation :

>   MyQuery <- paste(" 
+    SELECT Tamis_Lavage.* 
+    FROM Tamis_Lavage
+    IN \"d:\\Mes Documents\\Pascal\\03 - BiomFix\\99 - Suivi Etude\\01 - Donnees\\Données STH\\Test_Import\\Copie de 20110623Acquisition.mdb\" "
+     , sep="")
>   
>   tmp <- sqlQuery(con, query=MyQuery)
>   tmp       
[1] "42000 -1002 [Microsoft][Pilote ODBC Microsoft Access] Le moteur de base de données ne peut pas trouver '[d:\\Mes Documents\\Pascal\\03 - BiomFix\\99 - Suivi Etude\\01 - Donnees\\Données STH\\Test_Import\\Copie de 20110623Acquisition.mdb]'. Assurez-vous que le nom de paramètre ou d'alias est valide, qu'il ne comprend pas de caractère ou de ponctuation incorrect et qu'il n'est pas trop long."
[2] "[RODBC] ERROR: Could not SQLExecDirect ' SELECT Tamis_Lavage.* \n   FROM Tamis_Lavage\n   IN \"d:\\Mes Documents\\Pascal\\03 - BiomFix\\99 - Suivi Etude\\01 - Donnees\\Données STH\\Test_Import\\Copie de 20110623Acquisition.mdb\" '"                                                                                                                                                                  
>   MyQuery 
[1] " \n SELECT Tamis_Lavage.* \n   FROM Tamis_Lavage\n   IN \"d:\\Mes Documents\\Pascal\\03 - BiomFix\\99 - Suivi Etude\\01 - Donnees\\Données STH\\Test_Import\\Copie de 20110623Acquisition.mdb\" "

leads to an error.

Could you help in the translation process ?

Best regards Pascal

Your MyQuery could be problematic because of the newlines \\n that you introduced.

Try the following:

MyQuery <- paste(
    "SELECT Tamis_Lavage.*", 
    "FROM Tamis_Lavage",
    "IN 'd:\\Mes Documents\\Pascal\\03 - BiomFix\\99 - Suivi Etude\\01 - Donnees\\Données STH\\Test_Import\\Copie de 20110623Acquisition.mdb'")

What is different?

  • None of the parameters to paste contains a \\n .
  • I find it easier to use single quotes ' when working embedding quotes in strings. (This also has the benefit that you don't have to escape the quotes.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM