r使用rsqlite和dbplyr,从DBI软件包中的函数中找到SQL数据库中的表格

发布于 2025-01-21 18:09:21 字数 1281 浏览 4 评论 0原文

我一直在研究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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩一世无双 2025-01-28 18:09:21

表名是sq_mtcars - ie 选择语句是针对特定table的,并且该连接已与数据库

DBI::dbGetQuery(con, '
SELECT sql_mtcars.mpg 
FROM sql_mtcars
  ')

-Output

  mpg
1  21.0
2  21.0
3  22.8
4  21.4
5  18.7
6  18.1
7  14.3
8  24.4
9  22.8
10 19.2
11 17.8
...

建立怀疑,列出表格

dbListTables(con)
[1] "sql_mtcars"   "sqlite_stat1" "sqlite_stat4"

The table name is sq_mtcars- i.e. the select statement is for the particular table and the connection is already established with the database

DBI::dbGetQuery(con, '
SELECT sql_mtcars.mpg 
FROM sql_mtcars
  ')

-output

  mpg
1  21.0
2  21.0
3  22.8
4  21.4
5  18.7
6  18.1
7  14.3
8  24.4
9  22.8
10 19.2
11 17.8
...

If there are doubts, list the tables with

dbListTables(con)
[1] "sql_mtcars"   "sqlite_stat1" "sqlite_stat4"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文