- sql = "SELECT * FROM Whatever"
- client = TinyTds::Client.new(:username => "myUsername", :password => "myPassword", :dataserver => "myServer", :database => "myDatabase")
- result = client.execute(sql)
- results = result.each(:symbolize_keys => true, :as => :array, :cache_rows => true, :empty_sets => true) do |rowset| end
- #THIS IS TO OUTPUT IT TO THE CONSOLE
- for i in (0..result.fields.length)
- printf("%14s", result.fields[i])
- end
- for j in (0...result.affected_rows)
- puts ""
- for i in (0...result.fields.length)
- printf("%14s",results[j].at(i))
- end
- end
1. This is your SQL Statement, pretty self-explanatory. If you need help with SQL, a great place to start is here.
2. This is where the magic happens, logging into and connecting to the database. This was the simplest gem I could find to connect to the database, we use SQL Server (fyi).
3. Also very readable, if you need help with what 'execute' does, you can read about TinyTDS's syntax here. Basically it runs the sql query.
4.
6-8. This will print out the column headers from the table you just created.
9-14. This will print out each row, one by one.
This post is in the process of being written
This was incredibly helpful. Thanks!
ReplyDeleteHow did you find out about :symbolize_keys :as :cache_rows and :empty_sets?
ReplyDeletethank you for nice information.
ReplyDeletelearn ruby