Solved

Perl Script CSV to Access Database

Posted on 2010-11-19
2
685 Views
Last Modified: 2012-05-10
I am looking for a perl script to read a CSV file into an Access Database.

Anyone have anything that would work?

0
Comment
Question by:kyle972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Expert Comment

by:drhamel69
ID: 34178013
Does it have to be perl?  How about this for VB script (windows)

Dim objConn

Dim Cells(30,1000)
ReadCsv("c:\file.csv")

Set objConn = CreateObject("ADODB.Connection")
objConn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\MyAccessDatabase.mdb;­ "

For i = 1 to UBound(Cells)
    If Cells(1, i) <> "" Then
        'The cell isnt emtpy so lets do some work on this row...
        objConn.Execute("INSERT INTO table (column_1, column_2, column_n) VALUES ('" & cells(1,i) & "', '" & Cells(2,i) & "', '" & Cells(3,i)"');")
    End IF
Next

objConn.Close
Set objConn = Nothing

Sub ReadCSV(sFilename)
    Dim fso, f, line, Column, CellStart, CellEnd, Row
    Set fso = CreateObject("Scripting.FileSystem­ Object")
    Set f = fso.OpenTextFile(sFilename, 1)
    Row = 0
    While Not f.AtEndOfStream
        line = """" & Replace(f.ReadLine, ",", """,""") & """"
        Row = Row + 1
        Column = 0
        While line <> ""
            Column = Column + 1
            CellStart = 2
            CellEnd = InStr(2, line, """", vbTextCompare)
            Cells(Row, Column) = Mid(line, CellStart, CellEnd - CellStart)
            line = Mid(line, CellEnd + 2)
        Wend
        RowCount = Row
    Wend
    f.Close
    Set f = Nothing
    Set fso = Nothing
End Sub

Open in new window

0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34188392
Assuming you are in Windows environment. You need Win32::ODBC perl module installed. See an example below.
#! c:\perl\bin\perl

use Win32::ODBC;

$DriverType = "Microsoft Access Driver (*.mdb)";
$DSN = "Win32 ODBC";
$Dir = "V:\\Doc\\KB\\Tech\\ExpExch\\acc";
$DBase = "Database11.mdb";


Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType,("DSN=$DSN", "Description=Win32 ODBC Test DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD=")) or die "ConfigDSN(): Could not add temporary DSN" . Win32::ODBC::Error();

$db=new Win32::ODBC($DSN) or die "couldn't ODBC $DSN because ", Win32::ODBC::Error(), "\n";

$query = "insert into table1 values (4, 'Hello', 'World')";
!$db->Sql($query) or die "couldn't do $query because ", $db->Error(), "\n";

while($db->FetchRow())
{
 my %Data = $db->DataHash();
 foreach my $key(keys(%Data)){
   print $key," -> ",$Data{$key};
 }  
}


Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN") or die "ConfigDSN(): Could not remove temporary DSN because ", Win32::ODBC::Error();

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question