TCS
TCS

Reputation: 471

DROP TABLE command in dplyr

I am trying to remove a table from a database using dplyr in R. In SQLite the way to do this is clear: DROP TABLE...however when I search for this command in the dplyr docs I find nothing and no suggestion that this command is available. Is it? How?

Upvotes: 3

Views: 4870

Answers (1)

MattV
MattV

Reputation: 1383

As noted in the comments before, there are general database commands not really documented, among which ?db_drop_table.

To use it you need a connection object, this connection is attached to your database definition object:

pg <- src_postgres(dbname='NameOfDatabase', # Define the connection
                   user='user',
                   password='password')
> str(pg)
List of 3
 $ con  :Formal class 'PostgreSQLConnection' [package "RPostgreSQL"] with 1 slot
  .. ..@ Id: int [1:2] 12044 2
 $ info :List of 8
  ..$ host           : chr ""
  ..$ port           : chr "5432"
  ...
  ..$ rsId           : list()
 $ disco:<environment: 0x0000000011160fd8> 
 - attr(*, "class")= chr [1:3] "src_postgres" "src_sql" "src"    

So, in order to delete a table:

pg$con %>% db_drop_table(table='Tablename') # This drops the table 'Tablename'.

Upvotes: 9

Related Questions