Code Monkey home page Code Monkey logo

sdb's People

Contributors

valcu avatar

Watchers

 avatar  avatar

sdb's Issues

dbSafeWriteTable: Can't subset columns that don't exist.

Hi,

I had the problem of appended ~12M records to a MariaDB database and hit the "Got a packet bigger than 'max_allowed_packet' bytes" problem. Even increasing max_allowed_packet to maximum of 1073741824 didn't help.

I was going to upload the data in chunks, but your dbSafeWriteTable() looked like a simple solution. However, I found a few bugs that I fixed to get it to work in my case.

The first was "Can't subset columns that don't exist."

sdb::dbSafeWriteTable(con, "my_table", my_df, chunksize = 100000, append = T, overwrite = F)

r: Can't subset columns that don't exist.
x Locations 12, 13, 14, 15, 16, etc. don't exist.
ℹ There are only 11 columns.
Run `rlang::last_error()` to see where the error occurred.

The problem seems to be the sub-setting in the dbSafeWriteTable() function:

z = x[ (ii[[i]]) ]

It is missing the ','.

After I fixed this, I got a generic error:

Error in connection_sql_tables(conn@ptr, table_name = name) : 
  nanodbc/nanodbc.cpp:1347: 

Debugging showed it occurred at:

o[i] = RMariaDB::dbWriteTable( conn = con, name = name, value = z, append = TRUE, row.names = FALSE, ...)

Changing this line to the one below fixed the problem.

o[i] = DBI::dbWriteTable(conn = con, name = name, value = z, append = TRUE, row.names = FALSE, ...)

Something to do with scoping?

Here is the complete function that I used:

dbSafeWriteTableFixed <- function(con, name, x, append = TRUE, chunkSize = 1000, verbose = TRUE, ...) {
  
  n = nrow(x)
  i.to.n = 1:n
  ii = split(i.to.n, ceiling(seq_along(i.to.n)/chunkSize) )
  
  o = vector(length = length(ii))
  
  if(verbose) pb = txtProgressBar(max = length(ii), style = 3)
  for(i in 1:length(ii) )   {
    
    # z = x[ (ii[[i]]) ]
    z = x[ (ii[[i]]), ]
    
    # o[i] = RMariaDB::dbWriteTable(conn = con, name = name, value = z, append = TRUE, row.names = FALSE, ...)
    o[i] = DBI::dbWriteTable(conn = con, name = name, value = z, append = TRUE, row.names = FALSE, ...)
    if(verbose) setTxtProgressBar(pb, i)
    
  }
  
  all(o)
  
}

I hope this helps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.