Is it possible to insert (add) a row to a SQLite db table using dplyr package?(是否可以使用 dplyr 包向 SQLite db 表插入(添加)一行?)
问题描述
我对 dplyr 包的数据库连接功能不熟悉,但我对将其用于 SQLite 连接非常感兴趣.我按照 本教程 创建了一个 SQLite 数据库(my_db)
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)
并插入一个数据框(df)作为该数据库的表(my_table).
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))
有谁知道是否可以使用 dplyr 向现有的 sqlite 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!
推荐答案
您可以对通过 dplyr 创建的数据库/表执行 SQL 操作,但您必须还原到 RSQLite/DBI 调用并更改您制作数据库/表的方式:
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 db 表插入(添加)一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以使用 dplyr 包向 SQLite db 表插入(添加)一行?
基础教程推荐
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
