error using sqlite3 with Ruby on Windows - ruby

I have Ruby 2.2.2p95 on Windows. I am not using Rails. I am able to use the following to install sqlite3
gem install sqlite3
When I crate a Ruby file as follows, it fails to load sqlite3:
test.rb
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'test.db'
)
When I run
ruby test.rb
it throws an error from the line where I have the require statement for sqlite3:
cannot load such file -- sqlite3/sqlite3_native (LoadError)
I have sqlite installed on Windows and was able to create a table in the database. Any ideas on fixing this?

Related

How to use old gem versions with ruby?

Gem info - ruby 1.8.7, pg 0.9.0, postgres 0.8.1
All super old gems. I can't change that.
Code -
require 'rubygems'
require 'active_record'
require 'pg'
require 'postgres'
conn = PGconn.connect(:host => 'db.com', :port => 1234, :username => 'db1', :password => 'db1')
conn = PGconn.open
res = conn.exec("select * from a;")
PGconn.close
Require postgres line causes the following message and does not allow the code to execute. How do I make it work ? The warning is given below:
This is an old, deprecated version of the Ruby PostgreSQL driver that hasn't
been maintained or supported since early 2008.
You should install/require 'pg' instead.
If you need the 'postgres' gem for legacy code that can't be converted, you can
still install it using an explicit version, like so:
gem install postgres -v '0.7.9.2008.01.28'
gem uninstall postgres -v '>0.7.9.2008.01.28'
If you have any questions, the nice folks in the Google group can help:
http://goo.gl/OjOPP / ruby-pg#googlegroups.com

Ruby not finding gems, but they appear on gem list, and work in irb console?

I've got a Ruby app using deamon-kit to create a daemon that runs a cron task every 3 seconds.
Problem is I'm trying to add some error checking using Errbit, so that requires me to:
require 'hoptoad_notifier'
in my script. However, the script is complaining it can't find the file?
.rvm/gems/ruby-1.9.2-p320#stitch/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require': no such file to load -- hoptoad_notifier (LoadError)
What confuses me is that the gem is installed, when I run
gem list | grep hoptoad_notifier
I get
hoptoad_notifier (2.4.11)
Another test I did was to pop into irb console, on the same terminal window, after making sure I'm inside the correct RVM gemset off course:
1.9.2p320 :001 > require 'hoptoad_notifier'
=> true
1.9.2p320 :002 >
And voila, hoptoad is loading. It's only when loading my deamon-kit deamon, that I get the error.
What further confuses me is that when I look at my require block:
require 'rubygems'
require 'resque'
require 'hoptoad_notifier'
It's finding rubygems and resque, but not hoptoad_notifier? Why, when I comment out hoptoad, it doesn't also complain about resque and rubygems?
dameon-kit uses bundler, so you don't need to include rubygems. Include the following lines to your Gemfile :
gem 'resque'
gem 'hoptoad_notifier'
Run bundle install
and include your gems as usual :
require 'resque'
require 'hoptoad_notifier'
It worked for me.

Writing to an XML file in JRuby using Builder

I'm trying to write to an XML file using Builder but cannot seem to find Builder when I start up my program. I installed it using gem install builder and then used the following in my file, but it does not start up:
require 'rubygems'
require_gem 'builder'
def product_xml
builder = Builder::XmlMarkup.new("", 2)
puts builder.person {
name("Selene")
id("1")
x("2");
y("3");
}
end
The error that I am getting is:
LoadError: no such file to load -- rubygems
require at org/jruby/RubyKernel.java:1038
(root) at C:\Users\Ron\Dropbox\...\server\.\data\plugins\cmd-dev\dev.r
b:2
To install gems inside the JRuby enviroment you must use jruby -S gem, not just gem (if you are not using RVM).
So you should install Builder with jruby -S gem install builder. Once you have installed it, you should just use require 'builder', not require_gem 'builder'.

Error loading Active Record gem with sinatra app using RVM

I set up a project level RVM gemset for a sinatra app I am starting that will connect to a local database with Active Record. In order to test it I tried to run the below test app:
test.rb
require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'activerecord'
class Article < ActiveRecord::Base
end
get '/' do
Test.establish_connection(
:adapter => "sqlite3",
:database => "hw.db"
)
Test.first.content
end
(Taken from the answer to this question: What's the best way to talk to a database while using Sinatra?)
When I run ruby -rubygems test.rb I get this error:
/Users/[user]/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- activerecord (LoadError)
I've already installed the Active Record gem and it shows up in gem list and rvm current displays the correct gemset. I am new to RVM and I think this is something to do with the it not having the correct load path but I feel like I've set everything up correctly so I'd appreciate suggestions on what's wrong. Thanks.
As far as I can tell require 'activerecord' has been deprecated. Try using
require 'active_record'
instead.
If you have not already installed the activerecord gem, you will also get that error:
Open a command prompt and run these commands in the terminal:
#Find if the active record gem is already installed on your computer:
gem query --local
#See the downloadable gems, and see if activerecord is still available:
gem query --remote --name-matches activerecord
#Install your gem:
gem install --remote activerecord
#See if it installed successfully and is in the installed gem list:
gem query --local
Here is some code that uses the ActiveRecord gem to see if everything is configured right:
#Ruby code
require 'active_record'
class Dog < ActiveRecord::Base
has_many :dog_tags
end
puts "activerecord gem is installed";
If everything is working, it will print "activerecord gem is installed" without any errors.

Problem when requiring 'Qt4' in Ruby 1.8.7

I have successfully installed:
Ruby 1.8.7-p334
Rubygems 1.7.2
rake 0.9.0
qtruby4 2.1.0 mswin32
Now the following block of code
require 'rubygems'
require 'Qt4'
gives me an error:
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:926:in report_activate_error': RubyGem version error: qtruby4(2.1.0 not >= 0) (Gem::LoadError)
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:244:inactivate_dep'
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:236:in activate'
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:213:intry_activate'
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:56:in `require'
from C:/Users/nick/Documents/NetBeansProjects/RubyApplication2/lib/main.rb:4
Everything good when requiring just 'rubygems'. My OS is Windows 7.
Make sure you require correct gem name
require 'Qt4' seems like little different as almost all the ruby gem names are in simple letters
isnt your gem name 'qtruby4' by any chance, if so try
require 'rubygems'
require 'qtruby4'
HTH
sameera

Resources