是否可以使用dplyr包将一行添加到SQLite数据库表中? [英] Is it possible to insert (add) a row to a SQLite db table using dplyr package?

查看:103
本文介绍了是否可以使用dplyr包将一行添加到SQLite数据库表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很熟悉dplyr包的数据库连接功能,但我非常希望将其用于SQLite连接。我遵循本教程并创建了一个SQLite数据库(my_db )

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

,并将数据框(df)作为此数据库的表(my_table)插入。

  copy_to(my_db,df,my_table)

现在我想在此表中插入新行。我尝试过这样的东西(是的,我必须承认,它甚至看起来不像有前途的...但是我仍然尝试一下):

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

有人知道如果使用dplyr向现有的sqlite数据库表添加行吗?或者你怎么处理这个问题?非常感谢您提前!

解决方案

您可以对通过 dplyr ,但您必须恢复为RSQLite / DBI调用,并更改数据库/表的创建方式:

 library(dplyr)

my_db< - src_sqlite(my_db.sqlite3,create = TRUE)
copy_to(my_db,iris,my_table temporary = FALSE)#需要将临时设置为FALSE

#从src_sqlite
#创建的对象中抓取数据库连接,并发出INSERT方式

res < - dbSendQuery(my_db $ con,
'INSERT INTO my_table VALUES(9.9,9.9,9.9,9.9,new)')


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!

解决方案

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")')

这篇关于是否可以使用dplyr包将一行添加到SQLite数据库表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆