Andrew
Andrew

Reputation: 1851

Accessing MySQL Stored Procedures in Ruby

I have been working on a Ruby web app using MySQL for the database. I heard from a friend that the mysql gem did not work well with stored procedures, so I decided to use the mysql2 gem along with the seqel gem.

I have a stored procedure in MySQL called AddUser(Username, Password). It works fine from MySQL. I want to access it using the gems for security reasons.

The sqeuel doc here only really covers how to access databases made with the sequel gem. My first-draft code looked something like this, but doesn't actually run the procedure

# Connect to the database
db = Sequel.mysql2(
    :host => '127.0.0.1', 
    :database => database, 
    :username => username, 
    :password => password)

# Call the sproc
db["User"]
db.call_sproc(:insert, "AddUser", [name, pass])

The error message is:

Sequel::DatabaseError

Mysql2::Error: Query was empty

Upvotes: 0

Views: 862

Answers (1)

mcfinnigan
mcfinnigan

Reputation: 11638

I'm unsure whether a PreparedStatement is actually capable of calling a stored procedure. Certainly in JDBC you'd have to use a CallableStatement to do this.

Since you're using Sequel, have you had a look at Sequel::DataSet::StoredProcedures (http://sequel.rubyforge.org/rdoc-adapters/classes/Sequel/Dataset/StoredProcedures.html) ?

Upvotes: 1

Related Questions