2016-01-12 26 views
1

我是通過R連接到Redshift的新手,我已經閱讀了其他問題,但嘗試創建表格時仍遇到錯誤。 我已經成功地建立了一個連接,我想建立成功的表:Redshift with R

redshiftcon <- dbConnect(mm, user="username", password="secret_password", 
        dbname="dbtable", host="hostname", port="portnumber") 


dbSendQuery(redshiftcon, 
"create table ss_playground.test_table (unique_id VARCHAR, 
category VARCHAR, 
name VARCHAR, 
number_min float);") 

<PostgreSQLResult:(70214,5,1)> 

然而,當我試圖檢查表是否存在,如果場在那裏,我得到以下信息:

dbExistsTable(redshiftcon, ss_playground.test_table) 

Error in is(object, Cl) : 
error in evaluating the argument 'name' in selecting a method for function 
'dbExistsTable': Error: object 'ss_playground.test_table' not found 

> dbExistsTable(redshiftcon, 'ss_playground.test_table') 
[1] FALSE 

我很困惑,因爲我認爲表已成功創建,但也無法在數據庫本身中找到它。 當我試圖再次發送,並創建它,我得到如下:

> dbSendQuery(redshiftcon, 
     "create table ss_playground.test_table (unique_id VARCHAR, 
category VARCHAR, 
name VARCHAR, 
number_min float);") 

Error in postgresqlExecStatement(conn, statement, ...) : 
RS-DBI driver: (could not Retrieve the result : ERROR: Relation 
'test_table' already exists) 

有什麼我失蹤?

請幫忙! 謝謝

+1

我不知道,但嘗試添加'承諾;'你創建表後。有可能您的更改是特定於會話的,您可能希望先提交它們。 – rohitkulky

回答

2

我認爲ss_playground不是此用戶/角色的默認架構。您可以將模式設置爲默認設置。有一個偷看here

要快速解決您的代碼,你可以嘗試:

dbExistsTable(redshiftcon, c("ss_playground","test_table")) 

或破解它作爲

any(grepl("test_table",dbListTables(redshiftcon))) 
+0

非常感謝您的幫助!用這段代碼,我發現實際上這個表存在。 '> dbExistsTable(redshiftcon,c(「ss_playground」,「test_table」)) [1]是否需要後續授予權限? – RCN

+1

假設你的用戶名可以訪問模式,只需運行SET搜索路徑= ss_playground,public;''。所以看起來像''rs < - dbSendQuery(conn = redshiftcon,「SET search_path = ss_playground,public;」)''。那麼你不需要做''c(schema,tabl_name)'',並且可以通過''dbExistsTable(redshiftcon,test_table)''來查詢。 –

+0

我不特別想公開整個架構,有沒有辦法只公開這個特定的表?我正在玩'dbSendQuery(redshiftcon,「grant select on test_table in schema ss_playground to readonly;」)'where readonly是另一個用戶 – RCN