2011-12-28 151 views
1

我是新手,試圖瞭解R和PostgreSQL如何相互通話。我最近只使用R進行數據分析,但現在我試圖直接從數據庫導入。我已經安裝了RPostgreSQL並已連接到我的數據庫,我可以看到所有表格,但我無法編輯它們。我想獲得一些數據了出來,但是當我運行RPostgreSQL錯誤關係「tablename」不存在

下面的代碼:

>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 

我的其他表名是也不算很好。 「Alarm_Reports」,「Configuration」,「Event_Details」,「Events」

有沒有什麼基本的關於解決我丟失的表格的問題?

感謝您的幫助。

+0

看起來您正在連接到錯誤的數據庫。 '錯誤:關係「事件」不存在「是一個來自PostgreSQL本身的錯誤消息。 – 2011-12-28 17:41:50

+3

如果您的表格實際上以大寫字母命名爲「Events」,則需要引用保護其名稱:嘗試從「Events」中選擇*。更好的是,保持表名小寫。 – 2011-12-28 17:42:32

+0

感謝Dirk,但在PostgreSQL中,我使用你的例子來理解,rs < - dbSendQuery(con,「select * from TableName」)是否存在特殊的代碼?將「活動」從「選擇*從活動中」分開 – ravikk 2011-12-28 17:45:50

回答

6

正如我在我的評論中提到的,可能需要引用引號。以下是我們的單元測試文件中的實際代碼:

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\"") 
+0

您是否在單元測試中使用包:testthat? – Spacedman 2011-12-28 19:06:12

+0

來自'tests /'目錄,使用了比較老的'tests/foo.R'和'tests/foo.Rout.save'比較。我在其他軟件包中使用了'RUnit'。 – 2011-12-28 19:07:36

相關問題