How do I create a database using Ruby's DBI?
I'm developing a small Ruby application that I'd like to work with both PostgreSQL and MySQL.
It seems like Ruby/DBI is the most straightforward way to do this; I don't want to install a heavyweight ORM like ActiveRecord or something. Unfortunately, it seems like Ruby/DBI makes it simple to connect if you already have a database but I can't figure out how to connect to a server without a DB and issue the CREATE DATABASE
command.
So far I have this:
dbh = DBI.connect("DBI::Pg", "username", "password")
but I get this error when I try and connect: Invalid Data Source Name
. Any idea of what I can do?
Also, if I can get a 'databaseless handle' I was开发者_StackOverflow中文版 wondering if there was a DBI method to select_db
and drop into a database to execute further queries. That's secondary though and I can live without it.
You should have a postgres
database so you could try:
dbh = DBI.connect('dbi:Pg:postgres', 'username', 'password')
And then execute your CREATE DATABASE
from there. Also, the first argument to connect
is supposed to be:
dbi:Driver:database_conn_args
Where Driver
is the database driver (case sensitive) and database_conn_args
is usually the database name.
精彩评论