Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread

 

By: Simon Macneall
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 12:33
Excellent.

Thank you very much.

Simon

By: Praveen Devarao
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 12:31
Hi Simon,

You can use the method adapter_name on connection [ ActiveRecord::Base.connection.adapter_name ] which returns the name of the adapter being used. Hence if the adapter_name method returns IBM_DB, you are connected to DB2 else to any other Database (in your case MySQL)

...
if ActiveRecord::Base.connection.adapter_name =~ /IBM_DB/
...
else
...
end
...

Thanks

Praveen

By: Simon Macneall
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 12:21
Hi Praveen,

Basically, we need to keep the application able to work with mysql as well as DB2. So what I was thinking was something along the lines of
if (connected_to_db2)
while (row = IBM_DB.fetch_array(res))
...
end
else
while (row = res.fetch_row)
...
end
end

Thanks
Simon

By: Praveen Devarao
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 12:13
Hi Simon,

Not sure in what context is the question being asked.

If you meant how to specify that you are connecting to DB2 from Rails, you will need to mention ibm_db as the adapter being used in the database.yml [which you are already aware].

If you meant from the driver programming perspective, the IBM_DB.connect API should make it clear. http://rubyibm.rubyforge.org/docs/driver/2.0.0/doc/classes/IBM_DB.html#M000001

Is this what you are looking for or something else? Could you let me know the exact scenario in which you want to mention that you are connecting to DB2.

Thanks

Praveen

By: Simon Macneall
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 11:13
Ah, that makes sense.

Is there a recommended way of telling that I am connecting to DB2?

By: Praveen Devarao
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 09:21
Hi Simon,

Adding some info. If you use the Rails way of querying the database, you will not need any changes. It is only in cases where you are trying to explicitly call the driver methods (as in your case), minor changes will be needed.

Thanks

Praveen

By: Praveen Devarao
RE: undefined method 'fetch_row' [ reply ]  
2010-07-16 08:59
Hi Simon,

You will need to make a changes in the code. To use the driver methods you will need to call the methods on the IBM_DB module.

ActiveRecord::Base.connection.execute(sql) returns a statement handle. To fetch the results contained in the statement handle you will need make a call as below

...
res = ActiveRecord::Base.connection.execute(sql)
ids = Array.new
while (row = IBM_DB.fetch_array(res))
...

Note: you will need to use fetch_array instead of fetch_row. Check the driver doc for more details on different drivers APIs http://rubyibm.rubyforge.org/docs/driver/2.0.0/doc/

Let me know if you need more info or clarifications.

Thanks

Praveen

By: Simon Macneall
undefined method 'fetch_row' [ reply ]  
2010-07-16 08:30
Hi, We are trying to make our existing rails application work on DB2, and I am getting the following error

NoMethodError (undefined method `fetch_row' for #<IBM_DB::Statement:0xb65772d4>):

when this code is run
query = 'select id from form_datas'
res = ActiveRecord::Base.connection.execute(sql)
ids = Array.new
while (row = res.fetch_row)
ids << row[0].to_i
end

This works using the standard mysql adapter. Is this a bug, or do I need to actually change the code to work on DB2?

Thanks
Simon