RSQLite警告:“ SQL变量太多”

时间:2018-09-09 21:17:24

标签: r dplyr r-dbi rsqlite

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

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

数据库显示了正确的固定数目(24)的列,并且应该有约190行。由于一次无法容纳很多行,因此每个条目(行)都被迭代地追加。

不幸的是,它在输入99时一直失败。但是,当我尝试仅将95至105行输入数据库时​​,它就可以工作。

    # 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)

SQLiteDB

那么,为什么只有24个变量却失败了?

1 个答案:

答案 0 :(得分:1)

愚蠢的是,有一个for循环正在全局分配。这具有每次迭代重新添加多个列的效果。 SQLite只是不会追加额外的列,因此直到插入太大后才会失败。

但是,在下面的简化示例中可以看到问题的令人困惑的性质。

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

编辑 要使插入失败:

    b <- mtcars[1, ]
    for(i in 1:1000){
        b <- cbind(b, mtcars[2, ])
    }