r使用rsqlite和dbplyr,从DBI软件包中的函数中找到SQL数据库中的表格
我一直在研究Andrew Couch的YouTube视频,介绍RSQLITE,DBI和DBPLYR套餐。 这是链接。
我遇到了一个错误,但是我不是确定发生了什么。 YouTube视频是一个不错的视频,但我认为AC并没有遇到我遇到的同一问题,因此我认为它在那里没有诊断出来。
我首先加载库:
library(dbplyr)
library(RSQLite)
library(DBI)
然后,我将mtcars
重命名为sql_mtcars
,然后创建与SQLITE数据库的连接,然后将我的DataFrame复制到该数据库。
sql_mtcars <- mtcars
con <- RSQLite::dbConnect(SQLite(), ":memory:")
dplyr::copy_to(con, sql_mtcars)
我将其变成表(我想是因为我必须吗?)
sql_mtcars_db <- tbl(con, "sql_mtcars")
练习编写查询,然后测试查询的SQL版本。
sql_mtcars_db %>%
dplyr::select(car, mpg, wt) %>%
dplyr::show_query()
但是这是我卡住的地方。现在我试图直接在 DBI的DbgetQuery函数。
DBI::dbGetQuery(con, '
SELECT sql_mtcars_db.mpg
FROM sql_mtcars_db
')
我会出现此错误消息的提示:
Error: no such table: sql_mtcars_db
但是这让我感到困惑,因为当我运行此代码来调用表时:
sql_mtcars_db
我可以看到该表在SQLITE数据库中。
I have been studying a YouTube video from Andrew Couch about RSQLite, DBI, and dbplyr packages. Here is the link.
I'm running into an error, however, and I'm not sure what is happening. The YouTube video is a good video but I don't think AC is running into the same issue I am having so I don't think it's diagnosed there.
I first load the libraries:
library(dbplyr)
library(RSQLite)
library(DBI)
I then rename mtcars
as sql_mtcars
and I create a connection to the SQLite database and copy my dataframe to that database.
sql_mtcars <- mtcars
con <- RSQLite::dbConnect(SQLite(), ":memory:")
dplyr::copy_to(con, sql_mtcars)
I turn it into a table (I guess because I have to?)
sql_mtcars_db <- tbl(con, "sql_mtcars")
I practice writing a query and then testing the SQL version of the query.
sql_mtcars_db %>%
dplyr::select(car, mpg, wt) %>%
dplyr::show_query()
But here is where I get stuck. Now I'm trying to write the SQL code directly under the
dbGetQuery function from DBI.
DBI::dbGetQuery(con, '
SELECT sql_mtcars_db.mpg
FROM sql_mtcars_db
')
I get prompted with this error message:
Error: no such table: sql_mtcars_db
But that's confusing to me because when I run this line of code to call the table:
sql_mtcars_db
I can see that the table is there in the SQLite database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表名是
sq_mtcars
- ie选择
语句是针对特定table
的,并且该连接已与数据库-Output
建立怀疑,列出表格
The table name is
sq_mtcars
- i.e. theselect
statement is for the particulartable
and the connection is already established with the database-output
If there are doubts, list the tables with