Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@jmysql/insert.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % INSERT inserts values into a single row of a table using JDBC driver | |
2 % specified by the input connection. | |
3 % | |
4 % Usage: message = insert(conn, table, 'field1', value1, 'field2', value2,...) | |
5 % | |
6 % M Hewitson | |
7 % | |
8 % 31-07-07 | |
9 % | |
10 % $Id: insert.m,v 1.4 2009/09/16 13:38:58 hewitson Exp $ | |
11 % | |
12 | |
13 function message = insert(conn, table, varargin) | |
14 | |
15 message = ''; | |
16 q = sprintf('INSERT INTO %s SET ', table); | |
17 | |
18 for j=1:2:length(varargin) | |
19 | |
20 col = varargin{j}; | |
21 val = varargin{j+1}; | |
22 if isnumeric(val) | |
23 if ~any(isnan(val)) | |
24 q = [q col '=' num2str(val) ',']; | |
25 end | |
26 elseif ischar(val) | |
27 q = [q col '=' '''' val '''' ',']; | |
28 else | |
29 error('### val class [%s]', class(val)) | |
30 end | |
31 | |
32 end | |
33 | |
34 q = deblank([q(1:end-1) ';']); | |
35 | |
36 if length(q) > 50 | |
37 qdisp = [q(1:50) '...']; | |
38 utils.helper.msg(utils.const.msg.PROC1, 'running query %s', qdisp); | |
39 else | |
40 utils.helper.msg(utils.const.msg.PROC1, 'running query %s', q); | |
41 end | |
42 | |
43 try | |
44 stmt = conn.createStatement; | |
45 stmt.execute(q); | |
46 catch Me | |
47 error('### insert: failed to execute query'); | |
48 end | |
49 end | |
50 |