write right method to be concise

P

Pen Ttt

there is a section of my program,it can run,
require 'mysql'
dbh =3D Mysql.real_connect("localhost", "root", "222222")
days.times do #days=3D10000
case code
when "hk" then
str=3D"use dzh_hk"
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
when "sh" then
str=3D"use dzh_sk"
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
when "sz" then
str=3D"use dzh_sz"
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
end
end
#others ommitted
i feel it's burdensome=EF=BC=8Cwant to make it be concise, i writ a m=
ethod
in my program

p1:
require 'mysql'
days.times do
def myinsert(code,data)
dbh =3D Mysql.real_connect("localhost", "root", "222222")
str=3D"use dzh_"+code
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
end
end
#others ommitted
Mysql::Error: Too many connections

p2:
require 'mysql'
dbh =3D Mysql.real_connect("localhost", "root", "222222")
days.times do
def myinsert(code,data)
str=3D"use dzh_"+code
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
end
end
#others ommitted
NameError: undefined local variable or method `dbh' for main:Object

would you mind tell me to write a right method to make it be concise?

-- =

Posted via http://www.ruby-forum.com/.=
 
B

Brian Candler

Pen Ttt wrote in post #971751:
there is a section of my program,it can run,
require 'mysql'
dbh =3D Mysql.real_connect("localhost", "root", "222222")
days.times do #days=3D10000
case code
when "hk" then
str=3D"use dzh_hk"
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)=
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
when "sh" then
str=3D"use dzh_sk"
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)=
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
when "sz" then
str=3D"use dzh_sz"
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)=
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
end
end
#others ommitted
i feel it's burdensome=EF=BC=8Cwant to make it be concise, i writ a= method
in my program

p1:
require 'mysql'
days.times do
def myinsert(code,data)
dbh =3D Mysql.real_connect("localhost", "root", "222222")
str=3D"use dzh_"+code
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
end
end
#others ommitted
Mysql::Error: Too many connections

p2:
require 'mysql'
dbh =3D Mysql.real_connect("localhost", "root", "222222")
days.times do
def myinsert(code,data)
str=3D"use dzh_"+code
dbh.query(str)
str_insert=3D"insert into quote (code,date,price)values (?,?,?)
st=3Ddbh.prepare(str_insert)
st.execute(data[0],data[1],data[2])
end
end
#others ommitted
NameError: undefined local variable or method `dbh' for main:Object

would you mind tell me to write a right method to make it be concise?

You have several choices:

1. Pass dbh as an extra parameter to myinsert()

2. Make a wrapper class which holds the handle

class SqlHelper
def initialize(dbh)
@dbh =3D dbh
end
def myinsert(code,data)
@dbh.query("use dhz_#{code}")
... etc
end
end

sql =3D SqlHelper.new(dbh)
sql.myinsert(code, data)

3. Use a global variable ($dbh) to hold your database handle. This is =

probably OK for a short script, but is poor practice when building a =

large program.

-- =

Posted via http://www.ruby-forum.com/.=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top