How to execute create table on fly


#1

dear all,

I’m a Rails newbie and i have some problem.

I’m using MySQL.

I want to excute some complexity query,

and I need to create some table to hold temp query result.

I want to know how to execute some SQL query such like

"CREATE TEMPORARY TABLE templist SELECT * FROM table2 "

I’ve tried to excute find_by_sql("create table templist select * …),

but it seem to return a nil object , so that rails will halt.

any tips is appreciated.


#2

I won’t hold you back, but perhaps you should think about you solution -
temp tables are always strange… Anyways,

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000728

ActiveRecord::Base.execute(sql, name=nil)

is your friend.


#3

Florian G. wrote:

I won’t hold you back, but perhaps you should think about you solution -
temp tables are always strange… Anyways,

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000728

ActiveRecord::Base.execute(sql, name=nil)

is your friend.

thanks. and I find “execute” in my “Agile” book.

However, I still don’t know how to “execute” it in my controller.

if I add this line:

List.execute (“drop table if exists testtable;”)

then i got a error:
"NoMethodError in LookupController#findcross

undefined method `execute’ for List:Class"

Any tips is appreciated.

-Feng


#4

feng wrote the following on 06.04.2007 15:25 :

thanks. and I find “execute” in my “Agile” book.

However, I still don’t know how to “execute” it in my controller.

if I add this line:

List.execute (“drop table if exists testtable;”)

List.connection.execute should do it.


#5

Lionel B. wrote:

feng wrote the following on 06.04.2007 15:25 :

thanks. and I find “execute” in my “Agile” book.

However, I still don’t know how to “execute” it in my controller.

if I add this line:

List.execute (“drop table if exists testtable;”)

List.connection.execute should do it.

it works!

thanks very very much ! :slight_smile: