DBI/ODBC question: how to create DB programmatically?

D

Dave Halliday

Hi,

I've been trying to figure out how to use ODBC with DBI. So far, I have
created a DSN for an existing DB and managed to read tables, update them,
etc, but I'd like to be able to create a new DB from scratch
programmatically (i.e. without any manual steps to create a DSN and add a
database).

Is there a way to do this from the DBI interface? I've found a few VB
examples and dug around in the ODBC source code, but I can't quite make the
leap from the VB code to the DBI API.

I'm using Windows XP and Ruby 1.8.0.

Thanks,

Dave Halliday
 
M

Michael Neumann

Hi,

I've been trying to figure out how to use ODBC with DBI. So far, I have
created a DSN for an existing DB and managed to read tables, update them,
etc, but I'd like to be able to create a new DB from scratch
programmatically (i.e. without any manual steps to create a DSN and add a
database).

There's no special command to create databases with DBI/ODBC. Usually,
you can create a database by issuing the "CREATE DATABASE ..." SQL
statement, but this will probably not create a DSN for you.
Is there a way to do this from the DBI interface? I've found a few VB
examples and dug around in the ODBC source code, but I can't quite make the
leap from the VB code to the DBI API.

Everything what you can do in VB should be possible in Ruby by using the
win32ole module (this is how the ADO database driver is implemented).
I'm using Windows XP and Ruby 1.8.0.

Have a look at the ADO database driver (part of DBI in directory
lib/dbd_ado). Look how it's using the ADODB.Connection COM object, and
try to translate the VB example into Ruby.

Hope this helps.


Regards,

Michael
 
C

Chris Morris

Dave said:
Hi,

I've been trying to figure out how to use ODBC with DBI. So far, I have
created a DSN for an existing DB and managed to read tables, update them,
etc, but I'd like to be able to create a new DB from scratch
programmatically (i.e. without any manual steps to create a DSN and add a
database).
Access:

cat = WIN32OLE.new('ADOX.Catalog')
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=#{@out_mdb_fn}"

SQL Server:

db = WIN32OLE.new("ADODB.Connection")
db.Open "Provider=SQLOLEDB;Data Source=myserver;Database=master;..."
db.execute("CREATE DATABASE mydb")
 
D

Dave Halliday

Thanks Chris. That's what I was looking for. I never thought of using
win32ole.

In the meantime, I did manage to create an Access database using the
undocumented add_dsn() method in the ODBC driver:

drv = ODBC::Driver.new
drv.name = "Microsoft Access Driver (*.mdb)"
drv.attrs = {"CREATE_DB" => ".\\test.mdb General"}
ODBC.add_dsn(drv)

Cheers,

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top