Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sqlite

r

dplyr

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 SQLite database (my_db)

my_db <- src_sqlite("my_db.sqlite3", create = T) 

and inserted a dataframe (df) as a table (my_table) of this database.

copy_to(my_db,df,"my_table") 

Now I want to insert new rows in this table. I tried something like this (and yes I must admit it doesn't even look like promising... but I still gave it a try):

collect(build_sql("INSERT INTO my_table VALUES (",newdf,")", con=my_db)) 

Does anyone know if adding rows to an existing sqlite db table is even possible using dplyr? Or how would you deal with this problem? Many thanks in advance!

like image 813
rdatasculptor Avatar asked Oct 25 '14 23:10

rdatasculptor


People also ask

Is used to insert new rows into SQLite database tables?

SQLite INSERT INTO Statement is used to add new rows of data into a table in the database.

How manually insert data in SQLite database?

If you want to inset the data manually(fully graphical) do the following: Go to the DDMS perspective. File explorer (tab-menu) Locate your db (/data/data/com.

What is the R package of SQLite?

RSQLite: SQLite Interface for R Embeds the SQLite database engine in R and provides an interface compliant with the DBI package. The source for the SQLite engine and for various extensions in a recent version is included.


1 Answers

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 
like image 73
StatSandwich Avatar answered Sep 20 '22 09:09

StatSandwich