I'm a newbie to all of this. I'm running a project that uses the sqlite3 Ruby gem, but for some reason one of the table exists already. The project contains a ".sql" and ".db" file, among others.
How can you "view" or manipulate this database if it's running from a sqlite3 gem? Specifically, can you bundle exec rake, etc on it?
From a Rails perspective Sqlite works the same as any other database such as PostreSQL or MySQL in that you can run rake tasks on it (e.g. rake db:create, or db:setup, db:reset, db:migrate, etc. all work as expected). You can also use the rails console to query for or insert new data via your active record model objects.
From a non-Rails perspective, Sqlite has various command-line and GUI interfaces that you can install to interact with a Sqlite database using SQL commands.
Related
I am Looking to import(Rather run the schema.sql and seed.sql) in to the SQL server database using sequel gem. Is it possible with the gem? I did some very basic things like creating a database, running few queries etc. But Can not find any example for importing SQL file.
Thanks
j
Sequel doesn't support taking a string/file of multiple SQL statements and running it. You need to provide each SQL statement as a separate query string. Some adapters may allow the submission of multiple SQL statements in a single query string, but it is undefined behavior that depends on which driver is being used.
I have been learning about databases and wanted to know, if is there a Datamapper method -or another "ruby" way?- for creating an empty database rather than using create database "bd_name"; in psql, in a Sinatra project?
What you should be doing is creating a Rakefile and defining a Rake task to create the database and also a task to run the migrations after you make any structural changes to your database.
With DataMapper, this is somewhat more work than with ActiveRecord (which provides it's own DB tasks).
Have a look at the Rakefile provided by the Padrino framework for inspiration.
I would like to use ActiveRecord in a project without Rails.
I would also like to specify the database connections in config/database.yml like Rails does.
There is already one answer how multiple database connections work with Rails.
ActiveRecord talk to two databases?
I was unsuccessful using this approach and I'm not sure why. I only get the error message "database configuration does not specify adapter".
Is there some kind of magic in Rails that reads the database.yml that is not in ActiveRecord?
I could read it myself but then I don't know how to feed it to ActiveRecord so that I can use establish_connection in each model.
I found out how it works. You just fill the ActiveRecord::Base.configurations hash and then it works.
http://apidock.com/rails/ActiveRecord/Base/configurations/class
I was making a web app to deploy using Heroku.com when I realized that the only database type they support is PostgreSQL. Up until now, my app (powered by the Ruby gem Sinatra) accessed the database via the .Sqlite method for the Sequel gem.
Here's my Ruby script from when I used Sequel to access the .db file via SQLite:
DB = Sequel.sqlite('mydatabase.db')
DB.create_table :mytable do
primary_key :id
String :column_name
end
I installed PostgreSQL after learning Heroku used only that. Here's the script via postgres (my username is literally 'postgress', though I obviously won't reveal my password in this question):
DB = Sequel.postgres('mydatabase.db',:user=>'postgres',:password=>'my_password_here',:host=>'localhost',:port=>5432,:max_connections=>10)
DB.create_table :mytable do
primary_key :id
String :column_name
end
However, when I run this code, I get the following error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/sequel-3.38.0/lib/sequel/adapters/postgres.rb:208:in 'initialize': PG::Error: FATAL: database "mydatabase.db" does not exist (Sequel::DatabaseConnectionError)
I've tried searching Google, StackOverflow, Sequel documents, and the Heroku help documents for any help, but I've found no fix to this problem.
Does anyone know what I am doing wrong?
The database mydatabase.db doesn't exist, as per the error message from Pg. Likely reasons:
You probably meant mydatabase without the SQLite-specific .db filename suffix
It's possible you created the db with different case, eg "Mydatabase.db";
You might be connecting to a different Pg server than you think you are
You never created the database. Unlke SQLite's default behaviour, Pg doesn't create databases when you try to connect to a database that doesn't exist yet.
If in doubt, connect to Pg with psql and run \l to list databases, or connect via PgAdmin-III.
The PostgreSQL documentation and tutorial are highly recommended, too. They're well written and will teach you a lot about SQL in general as well as Pg in particular.
BTW, the postgres user is a superuser. You should not be using it for your application; it's like running your server as root, ie a really bad idea. Create a new PostgreSQL user without superuser, createdb or createuser rights and use that for your application. You can either CREATE DATABASE somedb WITH OWNER myappuser - or preferably, create the database owned by a different user to your webapp user and then expicitly GRANT the webapp user the minimum required permissions. See user management and GRANT.
On heroku all you need to do is tell Sequel to connect to the content of the DATABASE_URL environment variable (which is a properly formed url that Sequel understands):
DB = Sequel.connect(ENV['DATABASE_URL'])
See "UPDATE" below for what I now know is the crux of the problem.
I have a legacy Derby database that I want to make a Rails application to interface with.
I am using RVM so here are the steps I took:
1. rvm install jruby (installed 1.6.7.2)
2. rvm use jruby
3. gem install rails
4. rails new myapp
5. add "gem 'activerecord-jdbcderby-adapter'" to Gemfile of new rails app
6. bundle
7. copied my derby db folder (named 'perm') under db in the rails app
8. changed database.yml as follows:
development:
adapter: jdbcderby
database: db/perm
Then I made a model file and used set_table_name to set it to one of the table names and when I run rails c I get an exception that the table does not exist.
Also in the console when I do
ActiveRecord::Base.connection.tables
the only table that comes back is "schema_migrations".
I know there is nothing wrong with the database as I can connect to this exact copy and see all the tables using SquirrelSQL. Also, the rails app is connecting in some way since when I open the console I cannot, while the console is running, connect to the same instance using SquirrelSQL, and vice versa.
Does anyone have any suggestion as to why active record doesn't see the tables?
UPDATE:
The problem has something to do with how derby "organizes" tables into multiple "schemas" in the same file. When I create a new table from rails (i.e. with a migration) the table ends up in the "SA" schema. All of my legacy tables are in the "APP" schema (maybe I could move them but I don't want to do that if I can avoid it...other apps would break). So when I access the db from rails this way it's like only the "SA" schema exists. How do I tell Rails to 'use' the "App" schema (early on I tried prefacing the table name but that didn't work)?
I retitled the question accordingly.
UPDATE #2:
Apparently the jdbcderby gem supports the "schema" setting. On a guess I tried changing my database.yml to the following:
development:
adapter: jdbcderby
database: db/perm
schema: app (also tried APP)
When I have the app (or APP) schema setting when I do ActiveRecord::Base.connection.tables in the console I get the list of tables from the app schema (the list shows up with table names all lower case with no schema name).
But I am still having trouble accessing the tables. When I make a model file on an existing table and try to access it I get a JDBCError: "Schema 'SA' does not exist". I have tried various set_table_name calls with no success.
As far as I know there is nothing unusual about my database. But there is no information anywhere on how to do this. Am I the only person on earth who has ever tried to use a legacy Derby database with Rails?
The database name 'db/perm' is being interpreted relative to your current working directory, which is probably not the directory that you think it is. Try either (1) searching your hard disk for the file 'derby.log', which is probably being created in the actual current working directory of your rails app, or (2) specifying the absolute file path to your derby database, not a relative file path.
If the problem is the schema, Derby supports the SET SCHEMA statement: http://db.apache.org/derby/docs/10.8/ref/rrefsqlj32268.html
If you don't explicitly set the schema, it defaults to the username that you connect with, so you can also indirectly set the schema by logging in as the desired username.