Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Create a MS Access database programmatically in perl

by Anonymous Monk
on Jul 01, 2003 at 15:57 UTC ( [id://270517]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way I can programmatically create a MS Access database in PERL by reading a text file?

Each line in the text file is '|' delimited and represents a record. The top line in the text file contains the field names separated by '|".

Any help will be greatly appreciated

Replies are listed 'Best First'.
Re: Create a MS Access database programmatically in perl
by jsprat (Curate) on Jul 01, 2003 at 17:54 UTC
    Update:Added DBI method, much simpler way to create the tables!

    You don't mention what version of Access you are using, so the two examples may need tweaking based on the version of JET you have. I'll let you parse the '|' delimited data ;)

    Methods 2 and 3 create a database called 'new.mdb' in the current directory and add a table. Both methods were tested on Win2K with Access 97 (I know, old stuff ;)

    Method 1 - Create the db either way (or just copy an empty mdb file), then use DBI to add the tables and populate the data

    use DBI; my $datasource = "driver=Microsoft Access Driver (*.mdb);dbq=new.mdb"; my $username = 'admin'; my $password = ''; my $dbh = DBI->connect("dbi:ODBC:$datasource", $username, $password) || die "Error connecting: $!"; my $sql = "CREATE TABLE DBITable (FirstName TEXT, LastName TEXT)"; my $sth = $dbh->prepare($sql); $sth->execute;

    Method 2 - Use ADOX (requires MDAC)

    use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft ADO Ext. 2.5 for DDL and Security'; # The "catalog" is the db my $cat = Win32::OLE->new('ADOX.Catalog'); # Tweak the following line for your JetDB $cat->Create('Provider=Microsoft.Jet.OLEDB.3.51;Data Source=new.mdb'); #first create the table, modify it, then append it to the #tables collection my $table = Win32::OLE->new('ADOX.Table'); $table->{Name} = 'MyADOTable'; $table->Columns->Append('ID', adInteger); $table->Columns->Append('Desc', adVarChar); $cat->Tables->Append($table);

    Method 3 - Use DAO

    use strict; use warnings; use Win32::OLE; # The next two lines would need to be tweaked # Off the top of my head, I think DAO 3.6 (or 4.0?)is for Access 2000 # Look in HKEY_CLASSES_ROOT for DAO for the actual numbers use Win32::OLE::Const 'Microsoft DAO 3.51 Object Library'; my $acc = Win32::OLE->new("DAO.DBEngine.35", 'quit'); my $db = $acc->CreateDatabase('new.mdb', dbLangGeneral); my $tab = $db->CreateTableDef('MyDAOTable'); my $fld = $tab->CreateField('ID', dbInteger); $tab->Fields->Append($fld); $fld = $tab->CreateField('Desc', dbText); $tab->Fields->Append($fld); $db->TableDefs->Append($tab); $db->TableDefs->Refresh;

    Activestate's OLE browser will give you some clues as to available objects, methods and constants. The help files that came with Access will documents the object model in detail, with VBA examples.

    Good luck!

      Thank you! I appreciate your taking the time to outline possible solutions.This will be a good starting point for me.
Re: Create a MS Access database programmatically in perl
by particle (Vicar) on Jul 01, 2003 at 16:19 UTC
Re: Create a MS Access database programmatically in perl
by meredith (Friar) on Jul 01, 2003 at 16:09 UTC
    Unfortunately, you can't _directly_ produce an access database, but you should look at two modules: Win32::OLE and Win32::ODBC With OLE, you can create an Access.Application object and build your DB. Hmm, one other possibility is that DBI supports a file-based DB that Access also does. You may try browsing the DBD modules to see if that's possible.

    I'd personally use Win32::ODBC. You should be able to find more info or examples on http://www.roth.net You may also find the Super Search enlightening; as the question comes up infrequently.
    HTH! :)

    mhoward - at - hattmoward.org

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://270517]
Approved by tilly
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found