Question : Ruby - Insert Struct to MySql

I have a  struct  that I'd like to Insert into a MySql database.

I can do it when I hand-write a string to be inserted, but can't quite get my head
wrapped around how to insert the contents of data-structures.

Below is my class so far; but (I think) I'd want to accept an argument for a struct
on the 'load_tbl' method. Right now, it just inserts dummy data.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
require "mysql"

class MY_DB
    def initialize
      @dbh = Mysql.real_connect("localhost", "testuser", "testpass", "traffic")
    end

    def close_tbl
      @dbh.close
    end

  def load_table
    @dbh.query("INSERT INTO test(id,
                              name,
                              dataSourceId,
                              tmcId,
                              type,
                              status,
                              speed,
                              volume,
                              longVolume,
                              occupancy,
                              lastUpdated)
                  VALUES
                    (1,2,3,4,5,6,7,8,9,10,11)")

  end
end

Answer : Ruby - Insert Struct to MySql

Well you can answer this question yourself just try.
arr = [1,2,3]
=> [1, 2, 3]
irb(main):003:0> "some tries #{arr.join(',')}"
=> "some tries 1,2,3"
irb(main):004:0>

So yes you can.
Random Solutions  
 
programming4us programming4us