Вопрос

This is my first attempt at using R to access data from within MS Access using ODBC.

The following query works:

id <- levels(assetid)[assetid[,1]][12]

qry <- "SELECT DriverName FROM Data WHERE ID = 'idofinterest'"
sqlQuery(con, qry)

However, I would like to know if there is a way to use the variable "id" in the "qry" statement (without using paste)? I have seen some statements on the web with $ and % signs - however I haven't had any success in using them.

Thanks.

Это было полезно?

Решение

Why don't you want to use paste? Anyway, sprintf is an alternative means of string munging.

qry <- sprintf("SELECT DriverName FROM Data WHERE ID = '%s'", id)
sqlQuery(con, qry)

Другие советы

Try fn$ from the gsubfn package :

> library(gsubfn)
> id <- "abc"
> fn$identity("SELECT DriverName FROM Data WHERE ID = '$id'")
[1] "SELECT DriverName FROM Data WHERE ID = 'abc'"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top