Home > m > mysql > ltpda_insert.m

ltpda_insert

PURPOSE ^

LTPDA_INSERT inserts values into a single row of a table using JDBC driver

SYNOPSIS ^

function message = ltpda_insert(conn, table, varargin)

DESCRIPTION ^

 LTPDA_INSERT inserts values into a single row of a table using JDBC driver
 specified by the input connection.
 
 Usage: message = ltpda_insert(conn, table, 'field1', value1, 'field2',
                               value2,...)
 
 M Hewitson
 
 31-07-07
 
 $Id: ltpda_insert.m,v 1.3 2008/03/24 19:31:14 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function message = ltpda_insert(conn, table, varargin)
0002 
0003 % LTPDA_INSERT inserts values into a single row of a table using JDBC driver
0004 % specified by the input connection.
0005 %
0006 % Usage: message = ltpda_insert(conn, table, 'field1', value1, 'field2',
0007 %                               value2,...)
0008 %
0009 % M Hewitson
0010 %
0011 % 31-07-07
0012 %
0013 % $Id: ltpda_insert.m,v 1.3 2008/03/24 19:31:14 hewitson Exp $
0014 %
0015 
0016 q = sprintf('INSERT INTO %s(',  table);
0017 for j=1:2:length(varargin)
0018   q = [q varargin{j} ','];
0019 end
0020 q = q(1:end-1);
0021 
0022 q = [q ') VALUES('];
0023 
0024 for j=2:2:length(varargin)
0025   v = varargin{j};  
0026   if ischar(v)
0027     val = '';
0028     for k=1:size(v, 1)
0029       val = [val  v(k,:)];
0030     end
0031   else
0032     val = v;
0033   end
0034   if ischar(val)
0035     q = [q '''' val '''' ','];
0036   elseif isnumeric(val)
0037     q = [q mat2str(val) ','];
0038   else
0039     error('unknown data type');
0040   end  
0041 end
0042 
0043 q = q(1:end-1);
0044 q = [q ');'];
0045 
0046 try
0047   curs = exec(conn, q);
0048   message = curs.Message;
0049   close(curs);
0050 catch
0051   error('### ltpda_insert: failed to execute query: %s\nServer returned %s', q, curs.Message);
0052 end

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003