0

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.

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.