Last active
December 11, 2015 03:28
-
-
Save mizanRahman/4537459 to your computer and use it in GitHub Desktop.
fetching data from Microsoft SQL Server with ruby using TinyTDS gem
This file contains 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 characters
require 'tiny_tds' | |
client = TinyTds::Client.new(:username => 'sa', :password => 'secret', :host => 'localhosts') | |
client.execute("use testdb").do | |
#following block gets all the table names of the database and prints name. | |
result = client.execute("SELECT * FROM sys.Tables") | |
result.each do |row| | |
p row['name'] | |
end | |
#following block get count of rows in a table. | |
result.do | |
result = client.execute("select count(*) as row_count from people") | |
first_row = result.first | |
p first_row['row_count'] | |
File.open('result.txt', 'w') do |file| | |
file.write(first_row['row_count']) | |
end | |
# File.open('result.txt', 'w') do |file| | |
# result.each { |row| file.write(row['row_count']) } | |
# end | |
# result.each do |row| | |
# p row['row_count'] | |
# # end | |
# result.do | |
# result = client.execute("select count(*) as row_count from people where age>20") | |
# row=result.each(:first => true) | |
# p row | |
client.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/rails-sqlserver/tiny_tds