-
-
Save ecleel/1765587 to your computer and use it in GitHub Desktop.
Revisions
-
Abdulaziz Al-Shetwi renamed this gist
Feb 8, 2012 . 1 changed file with 26 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,10 @@ require 'win32ole' require 'csv' mdb_file="c:/Sites/labs/50q.bok" class AccessDb attr_accessor :mdb, :connection, :data, :fields, :catalog def initialize(mdb=nil) @mdb = mdb @@ -15,26 +15,24 @@ def initialize(mdb=nil) end def open connection_string = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' #Access 2010 connection string # connection_string = 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' connection_string << @mdb @connection = WIN32OLE.new('ADODB.Connection') @connection.Open(connection_string) @catalog = WIN32OLE.new("ADOX.Catalog") @catalog.ActiveConnection = @connection # catalog.create(connection_string) # Source="#{@mdb_file}" end def query(sql) recordset = WIN32OLE.new('ADODB.Recordset') puts recordset.ole_methods recordset.Open(sql, @connection) @fields = [] recordset.Fields.each do |field| @@ -48,6 +46,12 @@ def query(sql) recordset.Close end def tables tables = [] @catalog.tables.each {|t| tables << t.name if t.type == "TABLE" } tables end def execute(sql) @connection.Execute(sql) end @@ -62,5 +66,12 @@ def close db = AccessDb.new(mdb_file) db.open # db.query("Select * from b1611") # puts db.connection.invoke "OpenSchema" , "b1611" puts db.tables # puts db.fields # puts db.data # puts db.execute "select * from tables" # db.close -
Abdulaziz Al-Shetwi renamed this gist
Feb 8, 2012 . 1 changed file with 0 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,3 @@ require 'rubygems' require 'win32ole' require 'csv' -
dtolj revised this gist
Jan 18, 2011 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,8 @@ # # # # # require 'rubygems' require 'win32ole' require 'csv' -
dtolj created this gist
Jan 18, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ require 'rubygems' require 'win32ole' require 'csv' mdb_file="c:/dtolj/ruby_projects/phone.accdb" class AccessDb attr_accessor :mdb, :connection, :data, :fields def initialize(mdb=nil) @mdb = mdb @connection = nil @data = nil @fields = nil end def open #connection_string = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' #Access 2010 connection string connection_string = 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' connection_string << @mdb #create connection @connection = WIN32OLE.new('ADODB.Connection') @connection.Open(connection_string) #Create new database cat = WIN32OLE.new("ADOX.Catalog") cat.ActiveConnection = @connection #catalog.create(connection_string) #Source="#{@mdb_file}" end def query(sql) recordset = WIN32OLE.new('ADODB.Recordset') recordset.Open(sql, @connection) @fields = [] recordset.Fields.each do |field| @fields << field.Name end begin @data = recordset.GetRows.transpose rescue @data = [] end recordset.Close end def execute(sql) @connection.Execute(sql) end def close @connection.Close end end #create empty ms access file file = File.open(mdb_file, File::RDWR|File::CREAT) db = AccessDb.new(mdb_file) db.open