首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RSQLite警告:“太多的SQL变量”

RSQLite警告:“太多的SQL变量”
EN

Stack Overflow用户
提问于 2018-09-09 21:17:23
回答 1查看 683关注 0票数 2

我在使用RSQLite时遇到了以下错误,并且在诊断问题时遇到了问题:

Error in result_create(conn@ptr, statement) : too many SQL variables

数据库显示了正确的、固定的列数(24),并且应该有190行。因为一次多行不适合RAM,所以每一个条目(行)都会被迭代地追加。

不幸的是,它在进入99时一直不及格。但是,当我试图只向数据库中输入第95行至第105行时,它可以工作。

代码语言:javascript
复制
    # Doesn't work

    samplesToAdd <- samplesToAdd[1:100, ]

    newDatabase2 <- DBI::dbConnect(RSQLite::SQLite(),
                           "C:/Users/chase/Documents/GitHub/IDBac_App/inst/app/SpectraLibrary/z1.sqlite")
    IDBacApp::addNewLibrary(samplesToAdd = samplesToAdd,
                    newDatabase = newDatabase2,
                    selectedIDBacDataFolder = selectedIDBacDataFolder)


    Warning: Error in result_create: too many SQL variables

    # Works  

    samplesToAdd <- samplesToAdd[95:105, ]

    newDatabase2 <- DBI::dbConnect(RSQLite::SQLite(),
                           "C:/Users/chase/Documents/GitHub/IDBac_App/inst/app/SpectraLibrary/z1.sqlite")
    IDBacApp::addNewLibrary(samplesToAdd = samplesToAdd,
                    newDatabase = newDatabase2,
                    selectedIDBacDataFolder = selectedIDBacDataFolder)

那么,为什么在只有24个变量的情况下,“变量太多”会失败呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-09 21:17:23

愚蠢的是,有一个for循环是全局分配的。这样做的效果是每次迭代都重新添加多个列。SQLite只是没有附加额外的列,所以在插入太大之前不会失败。

然而,这个问题的令人费解的性质可以在下面的简化例子中看到。

代码语言:javascript
复制
library(RSQLite)
library(magrittr)
library(dplyr)

a <- mtcars[1, ]
b <- cbind(mtcars[1, ], mtcars[2, ])


> as_tibble(a)
    # A tibble: 1 x 11
       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
    * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1    21     6   160   110   3.9  2.62  16.5     0     1     4     4
> as_tibble(b)

Error: Columns `mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb` must have unique names

# But "tibbble" wasn't used:
> b
          mpg cyl disp  hp drat   wt  qsec vs am gear carb mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4  21   6  160 110  3.9 2.875 17.02  0  1    4    4




con <- DBI::dbConnect(RSQLite::SQLite(), 
file.path(getwd(),"StackoverflowExample", "exw.sqlite"))

DBI::dbWriteTable(conn = con,
              name = "IDBacDatabase", # SQLite table to insert into
              a, # Insert single row into DB
              append = TRUE, # Append to existing table
              overwrite = FALSE) # Do not overwrite


DBI::dbWriteTable(conn = con,
              name = "IDBacDatabase", # SQLite table to insert into
              b, # Insert single row into DB
              append = TRUE, # Append to existing table
              overwrite = FALSE) # Do not overwrite



 db <- dplyr::tbl(con, "IDBacDatabase")

 db

 # Source:   table<IDBacDatabase> [?? x 11]
 # Database: sqlite 3.22.0           [C:\Users\chase\Documents\StackoverflowExample\ex.sqlite]
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1    21     6   160   110   3.9  2.62  16.5     0     1     4     4
 2    21     6   160   110   3.9  2.62  16.5     0     1     4     4

编辑使插入失败:

代码语言:javascript
复制
    b <- mtcars[1, ]
    for(i in 1:1000){
        b <- cbind(b, mtcars[2, ])
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52248722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档