Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Inserting data from an array into an ms access 2000 table

Reply
Thread Tools

Inserting data from an array into an ms access 2000 table

 
 
Cooper Deford
Guest
Posts: n/a
 
      07-20-2008
Good Afternoon,

I am trying to figure out how to get data from an array into an access
table.

On the rubyonwindows site I found the following code:

db = AccessDb.new('c:\Baseball\lahman54.mdb')
db.opendb.query("SELECT * FROM AllStar WHERE playerID =
'conceda01';")field_names = db.fields
rows = db.data
db.execute("INSERT INTO HallOfFame VALUES ('Dave', 'Concepcion');")
db.close

This code works fine if I break the array up into seperate elements but
I think there must be a simpler way.

this is how the array is built:

class <<Array
def multi(n, *args, &block)
if args.empty?
Array.new(n, &block)
else
Array.new(n) do
Array.multi(*args, &block)
end
end
end
end
card=Array.multi(24,24)


so the question is; How do i get the data loaded into this array into my
access table called wps in a database wps.mdb?

I have been trying to do something like this:

db.execute("INSERT INTO wps_test VALUES('card [$h]');"

Any help would be greatly appreciated.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
David Mullet
Guest
Posts: n/a
 
      07-22-2008
Cooper Deford wrote:
> Good Afternoon,
>
> I am trying to figure out how to get data from an array into an access
> table.
>
> On the rubyonwindows site I found the following code:
>
> db = AccessDb.new('c:\Baseball\lahman54.mdb')
> db.opendb.query("SELECT * FROM AllStar WHERE playerID =
> 'conceda01';")field_names = db.fields
> rows = db.data
> db.execute("INSERT INTO HallOfFame VALUES ('Dave', 'Concepcion');")
> db.close
>
> This code works fine if I break the array up into seperate elements but
> I think there must be a simpler way.
>
> this is how the array is built:
>
> class <<Array
> def multi(n, *args, &block)
> if args.empty?
> Array.new(n, &block)
> else
> Array.new(n) do
> Array.multi(*args, &block)
> end
> end
> end
> end
> card=Array.multi(24,24)
>
>
> so the question is; How do i get the data loaded into this array into my
> access table called wps in a database wps.mdb?
>
> I have been trying to do something like this:
>
> db.execute("INSERT INTO wps_test VALUES('card [$h]');"
>
> Any help would be greatly appreciated.


Assuming you are working with something like the AccessDb class defined
here...

http://rubyonwindows.blogspot.com/20...ms-access.html

...you could iterate over your array and generate/execute an SQL insert
statement, like this:

my_array.each do |a|
b = a.collect{|x| x = "'" + x + "'"}
c = b.join(",")
sql = "INSERT INTO MyTable VALUES (#{c});"
db.execute(sql)
end

The above code can certainly be improved upon, but hopefully gives you
an idea.

David

http://rubyonwindows.blogspot.com
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Cooper Deford
Guest
Posts: n/a
 
      07-22-2008
David,

Thanks very much for your response. This works just fine as is. I fooled
around quite a bit with join as part of the sql= statement but never
thought to iterate through the array. This is great.

Thanks again,

Cooper

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fetching & Inserting Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET Bhavesh ASP .Net 5 07-18-2007 07:39 AM
Fetching & Inserting Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET Bhavesh ASP .Net 1 07-17-2007 09:05 AM
Inserting & Fetching Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET Bhavesh ASP .Net 0 07-16-2007 11:15 AM
Inserting data into a table Geoff Wickens ASP General 1 08-25-2003 10:06 AM
Inserting into one table data from 2 tables and some input data. Renie83 ASP General 1 07-10-2003 08:29 PM



Advertisments