No BSD License  

txt2db

by

 

16 Dec 2002 (Updated )

Takes a delimited text file and populates a database table with it.

txt2db(sfilename,scoldelim,sdbname,sdbuser,sdbpassword,stlname,dinsupwip)
function txt2db(sfilename,scoldelim,sdbname,sdbuser,sdbpassword,stlname,dinsupwip)
% TXT2DB Reads a delimited text file in and populates a
% database table with it.
%
% sfilename     file name           string
% scoldelim     column delimeter    string
% sdbname       database name       string
% sdbuser       db user name        string
% sdbpassword   db password         string
% stlname       table name          string
% dinsupwip       insert,update,wipe  int
%
% REQUIRES dlmreadall

% IT'S NOT FANCY BUT IT WORKS
% Michael Robbins
% michael.robbins@us.cibc.com
% robbins@bloomberg.net

% DATABASE TYPES
BYTE = -6;
SMALLINT = 5;
INTEGER = 4;
REAL = 7;
DOUBLE = 8;
GUID = 1111;
CURRENCY = 2;
NUMERICTYPE = [BYTE SMALLINT INTEGER REAL DOUBLE GUID CURRENCY];

% GET DATABASE INFO
conn=database(sdbname,sdbuser,sdbpassword);
curs=cursor(conn,['SELECT * FROM ' stlname]);
curs=fetch(curs);
attrstruct=attr(curs);
sfields=columnnames(curs);
typenum=zeros(1,length(attrstruct));
for i=1:length(attrstruct)
    typenum(i)=any(attrstruct(i).typeValue==NUMERICTYPE);
end;

% GET DATA FROM FILE
celldata=dlmreadall(sfilename,scoldelim,typenum).';

% PUT DATA TO DATABASE
sfields=strrep(sfields,'''','');
if length(typenum)==1 sfields={sfields}; end;
switch dinsupwip
    case 1, insert(conn,stlname,sfields,celldata);
    case 2, update(conn,stlname,sfields,celldata);
    case 3, curs=cursor(conn,['DELETE * ' stlname]);
        insert(conn,stlname,sfields,celldata);
    otherwise, error('dinswip');
end;

Contact us