automatic import of .csv file into access database

  • Thread starter Thread starter amolchopra
  • Start date Start date
A

amolchopra

I have a .csv file. I have to read it and import the contents into one
of the DB tables. I have already worked on the ODBC part. Have opened
the file in read mode. This is a "C" application and the user has to
provide the files name at the command prompt. I can read the file name,
can open the file have connected to the DB. All I am looking for is
some command (or SQL query) that I can use in my C program to import
its contents to the table. Can some one please help??
 
My knowledge of C is very slender, but I would have thought that if you
have established an ODBC connection to the mdb file it's a matter of
consulting the documentation for whatever C library you're using to talk
to the ODBC driver.

For a command-line utility my own preference would be to use an
OLE-aware scripting language such as Perl or VBScript: sample code at
the end of this message.

I have a .csv file. I have to read it and import the contents into one
of the DB tables. I have already worked on the ODBC part. Have opened
the file in read mode. This is a "C" application and the user has to
provide the files name at the command prompt. I can read the file name,
can open the file have connected to the DB. All I am looking for is
some command (or SQL query) that I can use in my C program to import
its contents to the table. Can some one please help??

'-------------------------------------------------
'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].B1#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close
'-------------End of VBScript-----------------

#--------Sample Perl script appending a record to a table
use strict;
use Win32::OLE;

my $Jet; #DAO.DatabaseEngine
my $DB ; #DAO.Database

my $SQLquery = "INSERT INTO Details (SaleID, Comment)
VALUES (11, 'Test value from Perl');";

$Jet = Win32::OLE->CreateObject('DAO.DBEngine.36')
or die "Can't create Jet database engine.";

$DB = $Jet->OpenDatabase('C:\\Temp1\\BoxWithinBox_Backup.mdb');

$DB->Execute($SQLquery, 128); #128=DBFailOnError

$DB->Close;
#-----------------End of Perl script---------------
 
Back
Top