Is it possible to insert (add) a row to a SQLite db table using dplyr package?

后端 未结 4 1970
时光说笑
时光说笑 2020-12-09 08:20

I am new to the database connection capabilities of dplyr package, but I am very interested in using it for an SQLite connection. I followed this tutorial and created an SQL

相关标签:
4条回答
  • 2020-12-09 09:05

    In this newsgroup. Hadley explained the purpose of the function dplyr::copy_to(). It is intended to create temporary test tables. The email exchange ends by suggesting to use RMySQL::dbWriteTable() to append data to an existing table. The same applies to SQLite databases, as explained in the accepted answer above.

    To append a data frame dtf which has the same column names as an existing database table, I used:

    library(RMySQL)
    DB <- dbConnect(MySQL(), user="usename", host="localhost",
                       password="***", dbname="dbname")
    dbWriteTable(DB, "tablename", dtf, append=TRUE, row.names = FALSE)
    
    0 讨论(0)
  • 2020-12-09 09:08

    The main reason I use dplyr to write into a database is that I do not want to switch between languages mid code. For your question the best solution I think is to use the db_insert_into() function of dplyr as illustrated by StatSandwich

    0 讨论(0)
  • 2020-12-09 09:18

    You can perform SQL operations on a database/table created via dplyr, but you have to revert to RSQLite/DBI calls and change how you made the database/table:

    library(dplyr)
    
    my_db <- src_sqlite("my_db.sqlite3", create=TRUE) 
    copy_to(my_db, iris, "my_table", temporary=FALSE) # need to set temporary to FALSE
    
    # grab the db connection from the object created by src_sqlite
    # and issue the INSERT That way
    
    res <- dbSendQuery(my_db$con, 
                       'INSERT INTO my_table VALUES (9.9, 9.9, 9.9, 9.9, "new")')
    
    0 讨论(0)
  • 2020-12-09 09:18

    No, you can do this all within dplyr.

    require(dplyr)
    
    my_db <- src_sqlite( "my_db.sqlite3", create = TRUE)                 # create src
    copy_to( my_db, iris, "my_table", temporary = FALSE)                 # create table
    newdf = iris                                                         # create new data
    db_insert_into( con = my_db$con, table = "my_table", values = newdf) # insert into
    
    0 讨论(0)
提交回复
热议问题