Ruben Gregory
Ruben Gregory

Reputation: 41

R with postgresql database

I've been trying to query data from postgresql database (pgadmin) into R and analyse. Most of the queries work except when I try to write a condition specifically to filter out most of the rows. Please find the code below

dbGetQuery(con, 'select * from "db_name"."User" where "db_name"."User"."FirstName" = "Mani" ')

Error in result_create(conn@ptr, statement) : Failed to prepare query: ERROR: column "Mani" does not exist LINE 1: ...from "db_name"."User" where "db_name"."User"."FirstName" = "Mani" ^

this is the error I get, Why is it considering Mani as a column when it is just an element. Someone pls assist me

Upvotes: 2

Views: 176

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

String literals in Postgres (and most flavors of SQL) take single quotes. This, combined with a few other optimizations in your code leave us with this:

sql <- "select * from db_name.User u where u.FirstName = 'Mani'"
dbGetQuery(con, sql)

Note that introduced a table alias, for the User table, so that we don't have to repeat the fully qualified name in the WHERE clause.

Upvotes: 4

Related Questions