Wanted: MySQL Magic Chanting - No Fluff Just Stuff

Wanted: MySQL Magic Chanting

Posted by: Robert Fischer on April 12, 2010

Here’s my problem. I’m working on integrating the EPA MOVES model, which means I’m living on MySQL. I need to load large amounts of data to the database, so I’m using the MySQL “LOAD DATA INFILE” command. The problem is that while the data file format is always the same, the tables that I need to load are going to differ, as are the file locations.

Complicating the issue is that I’m calling this from GAMS, which means any kind of advanced programming stunts (like parsing the file or dynamically generating SQL on the client) are pretty much out.

So I’d like to do something like this on the server:

LOAD DATA INFILE @data_file INTO TABLE @table_name

Except that MySQL really dislikes the idea of using variables to specify the table name.

“No problem,” I think to myself, “I’ll generate dynamic SQL on the server using PREPARE.” (Yes, I do think in hyperlinks.) A bit of cutesy abuse of CONCAT and PREPARE later, I’m ready to roll.

The catch? ERROR 1295: MySQL doesn’t support LOAD DATA in prepared statements. Nerts.

At this point, I’m seriously considering automatically generating a stored procedure load_data_TABLENAME for every table name in the database, and that’s making me feel sad deep inside. An alternative is to add a layer of indirection via a call to .Net, but the result will split some of the database logic which I’d like to keep consolidated on the server, not to mention complicating a system that I’d much prefer to simplify.

Any suggestions on how to make this work?


Comments

  • April 12, 2010, James Moore wrote: Does loading into a temporary table and renaming help? The table rename might have different constraints than the load-data.
  • April 13, 2010, Robert Fischer wrote: Maybe. Will that allow me to parameterize the table names? i.e., could I do something like this:
    load data infile @data_file into table tmp_foo;
    rename table tmp_foo to @new_table_name;
    
    I suspect not, but maybe...?

This post was by Robert Fischer, written on April 12, 2010.
Comment on this post: http://enfranchisedmind.com/blog/posts/mysql-prepared-statement-pain/#respond
Public Permalink: http://enfranchisedmind.com/blog/posts/mysql-prepared-statement-pain/
Creative Commons License
This article was a post on the EnfranchisedMind blog. EnfranchisedMind Blog by Robert Fischer, Brian Hurt, and Other Authors is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

(Digital Fingerprint: bcecb67d74ab248f06f068724220e340 (68.142.243.103) )

Robert Fischer

About Robert Fischer

Robert Fischer is a multi-language open source developer currently specializing in Groovy in Grails. In the past, his specialties have been in Perl, Java, Ruby, and OCaml. In the future, his specialty will probably be F# or (preferably) a functional JVM language like Scala or Clojure.

Robert is the author of Grails Persistence in GORM and GSQL, a regular contributor to GroovyMag and JSMag, the founder of the JConch Java concurrency library, and the author/maintainer of Liquibase-DSL and the Autobase database migration plugin for Grails.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »