MySql Ruby Examples
Decided to learn a little more about Ruby by writing a Ruby program to manipulate some MySql tables that I have. I could have used "ActiveRecord" but I wanted to use Ruby classes only.
Originally I had errors because it was not finding the MySql gem. They may be some setting for in some configuration file, but the solution that works for me was to 'include rubygems'. Here is a basic Ruby program to connect to a database and select some information.
Code:
require 'rubygems' | |
require 'mysql' | |
| |
con = Mysql.new('localhost', 'root', '', 'db') | |
puts "Server version: " + con.get_server_info | |
stmt = "select id, name from users where id = 1" | |
rs = con.query(stmt) | |
puts "Number of rows returned: #{rs.num_rows}" |
Of course it would be nice to see the results. The reasonably written documentation has no examples so I have included them here. There are different ways of fetching rows, using each or each_hash. I prefer the hash because I like to use column names rather then numbers.
Code:
rs.each_hash do |row| | |
puts "ID:#{row['id']} Name:#{row['name']}." | |
end |
And here is each:
Code:
rs.each do |row| | |
puts "ID:#{row[0]} Name:#{row[1]}." | |
end |
Now that we have selected, lets try a couple of other functions:
Code:
# Now insert! | |
stmt = "Insert into users (name) values ('fred')" | |
con.query(stmt) | |
puts "Number of rows inserted: #{con.affected_rows}" | |
| |
# Now update | |
stmt = "update users set email='fred@flintstone.ca' where name='fred' " | |
con.query(stmt) | |
puts "Number of rows updated: #{con.affected_rows}" |
Of course you can also perform other functions like creating tables, adding columns, you get the idea. Just for added fun, here are some other snippets, this first one prints out fields names of a table, in this case the 'secrets' table followed by a list of databases:
Code:
con.list_fields('secrets').fetch_fields.each do |f| | |
puts "Field name : #{f.name} " | |
end | |
| |
con.list_dbs.each do |db| | |
puts "db = #{db} " | |
end |
Fabricating Robots
Working today on a Rails app that groups client generated webapges and we need to have the ability to screen out "child safety challenged" and "naughtyware" from being indexed by google so we are not blacklisted from adwords. The final step was generating the robots meta tag to either index or not. So I was able to use the line:
Code:
<meta name="robots" content="<%= fabricate_robots %>" /> |
After looking at that line, I suddenly really enjoyed the fact that it appears that I am fabricating robots...
Firefox Addon
Built my first firefox plugin! Fun to see something like this working. Here are the steps to just get a basic one up and running.
Turn on All Debugging
You should have:
In about:config set to true:
- extensions.logging.enabled
- javascript.options.showInConsole
- browser.dom.window.dump.enabled (note: you add this one!)
Setup an Projects directory under mozilla
win: \path2\Mozilla\Firefox\Projects\yourExtensionName
linux: ~/.mozilla/firefox/Projects\yourExtensionName
Note that Projects and Profiles are not to be confused! You may want a different Project/Profile name. I chose the same to know what project & what profile to use for testing!
Setup an Profiles directory under mozilla
win: \path2\Mozilla\Firefox\Profiles\yourExtensionName
linux: ~/.mozilla/firefox/Profile\yourExtensionName
Setup a profile on firefox
Place the profile you are about to create under the above created directories. To do this use Firefox's profile manager:
Code:
firefox firefox -p // win | |
(may have to set windows env var: MOZ_NO_REMOTE=1 ??? | |
firefox -ProfileManager -no-remote // Linux |
In my case I used the yourExtensionName as the profile name.
Setup Files Required
Google it to find the directory structure you need as well as the base content for the following base files:
- install.rdf
- chrome.manifest
- XUL files (this in directory content, its your coding!)
These files will be in the
Projects/yourExtensionName/
directory.
Development Tip
You will want Firefox to automatically load the addon each time you code changes to the extension, so in the folder
Projects/yourExtensionName/extensions
have a file name like:
extensionname@your.unique.name.
This should match the id tag in the install.rdf manifest file. Note you will have to restart firefox to pick up the changes.
Gotchas
In the install.rdf file make sure that the tags:
Code:
<em:minVersion>3.0</em:minVersion> | |
<em:maxVersion>3.6.4</em:maxVersion> |
allow for your version of Firefox!
Passing Ruby vars to Javascript
It may not be the best way of doing things, but it is the way I am doing it. I need to pass a Ruby variable to a javascript function, here's how:
<% @x = "#{params[:shortened_url]}" %>
<a href="javascript:task('<%= @x -%>')">Do it</a>
New Installation Headaches
I need to have a Linux instance running for a Rails app that I am to fix. That means I need Ruby, Rails, and MySql. Not sure yet about apache-passenger.
I like the command line, but since I have not used it much lately I am learning by being burnt by simple mistakes and just not knowing. When I know what I want to do and cannot do it because of some simple mistake, its frustrating. I have done all this on windows awhile ago and know how to do it easily & quickly. Now on Linux I want it to go just as fast and I don't do well knowing what has to be done and seeing very slow progress.
Headache city as I know more about software then installation of these. I am having trouble with mysql mostly, getting permissions and things working. I am trying to run:
Code:
mysqlimport -u clusterurl -p clusterurl_development clusterurl_production.backupfile.sql |
and am getting Access Denied errors. I did it quickly on the first attempt on my windows mysql installation, but the Linux one I'm setting permissions and there is a small piece missing somewhere as its late and I typed priviledge a dozen times instead of privilege. I've been at it a couple of hours.
I think I will post a little list of MySql commands that I know I will be using often.
Off to bed...
:: Next >>