ravikk
ravikk

Reputation: 13

RPostgreSQL error relation"tablename" does not exist

I am newbie trying to understand how R and PostgreSQL can talk to each other. I have used R lately only for data analysis but now, I am trying to import directly from a database. I have installed RPostgreSQL and have connected to my database, I can see all the tables but I cannot edit them. I would like get some data out of them but when I run the

the following code:

>query<-"SELECT * FROM Events"
> rs <- dbSendQuery(con,query)
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not Retrieve the result : ERROR:  relation "events" does not exist
LINE 1: SELECT * FROM Events

My other table names are not great either. "Alarm_Reports" ,"Configuration","Event_Details","Events"

Is there something basic about addressing the tables that I am missing?

Thanks for all the help.

Upvotes: 1

Views: 4794

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368629

As I mentioned in my comment, quotes may be needed. Here is actual code from one of our unit tests files:

res <- dbGetQuery(con, "create table Foo1 (f1 int)")
res <- dbGetQuery(con, "create table \"Foo2\" (f1 int)")

cat("Test should create foo1 and Foo2 tables\n")
## res <- dbGetQuery(con, paste("SELECT * FROM information_schema.tables",
##                              "WHERE table_schema = 'public'")
## print res

if (dbExistsTable(con, "Foo1")) {
    cat("FAIL - Foo1 Table exists.\n")
}
else {
    cat("Pass - Foo1 Table does not exist.\n")
}

if (dbExistsTable(con, "foo1")) {
    cat("Pass - foo1 Table exists.\n")
}
else {
    cat("FAIL - foo1 Table does not exist.\n")
}

if (dbExistsTable(con, "Foo2")) {
    cat("Pass - Foo2 Table exists.\n")
}
else {
    cat("FAIL - Foo2 Table does not exist.\n")
}

if (dbExistsTable(con, "foo2")) {
    cat("FAIL - foo2 Table exists.\n")
}
else {
    cat("Pass - foo2 Table does not exist.\n")
}

if (dbExistsTable(con, "\"Foo2\"")) {
    cat("FAIL - \"Foo2\" Table exists.\n")
}
else {
    cat("Pass - \"Foo2\" Table does not exist.\n")
}

if (dbExistsTable(con, "\"foo2\"")) {
    cat("FAIL - \"foo2\" Table exists.\n")
}
else {
    cat("Pass - \"foo2\" Table does not exist.\n")
}

res <- dbGetQuery(con, "drop table Foo1")
res <- dbGetQuery(con, "drop table \"Foo2\"")

Upvotes: 6

Related Questions