1 | function tbadd(table,cols,types,varargin) |
---|
2 | % TBADD Create a MySQL table |
---|
3 | % INPUTS : TABLE - table name, string |
---|
4 | % COLS - list of column names, (m x 1) or (1 x m) cell array |
---|
5 | % TYPES - list of column types, (m x 1) or (1 x m) cell array |
---|
6 | % OVER - string 'replace' if overwrites are allowed; if the |
---|
7 | % argument is omitted and TABLE already exists, TBADD |
---|
8 | % call will trigger an error |
---|
9 | % OUTPUTS : None |
---|
10 | % NOTES : Use DBASE.TABLE syntax to refer to a table not in the current |
---|
11 | % database |
---|
12 | % EXAMPLE : cols = {'customer','date','price'}; |
---|
13 | % types = {'varchar(30)', 'date', 'double'} |
---|
14 | % tbadd('orders',cols,types,'replace') |
---|
15 | % AUTHOR : Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 8/7/06 |
---|
16 | error(nargchk(3,4,nargin)) |
---|
17 | if nargin == 4 |
---|
18 | if ~strcmpi(varargin{1},'replace') |
---|
19 | error('Overwrites-allowed argument not recognized.') |
---|
20 | else |
---|
21 | overwrite = true; |
---|
22 | end |
---|
23 | else |
---|
24 | overwrite = false; |
---|
25 | end |
---|
26 | if ~mycheck |
---|
27 | error('No MySQL instance detected. Use MYOPEN to connect.') |
---|
28 | end |
---|
29 | try %#ok |
---|
30 | exists = false; |
---|
31 | [a,b,c,d,e,f] = mym(['describe ' table]); %#ok |
---|
32 | exists = true; |
---|
33 | end |
---|
34 | if exists |
---|
35 | if overwrite |
---|
36 | tbdrop(table) |
---|
37 | else |
---|
38 | error(['Table ' table ' already exists. Use ''replace'' to overwrite, or TBDROP to delete a table.']) |
---|
39 | end |
---|
40 | end |
---|
41 | K = length(cols); |
---|
42 | if ~iscell(cols) |
---|
43 | error('COLS must be a cell array.') |
---|
44 | end |
---|
45 | if ~iscell(types) |
---|
46 | error('TYPES must be a cell array.') |
---|
47 | end |
---|
48 | if ~isvector(cols) |
---|
49 | error('COLS must be a cell vector.') |
---|
50 | end |
---|
51 | if ~isvector(types) |
---|
52 | error('TYPES must be a cell vector.') |
---|
53 | end |
---|
54 | if K ~= length(types) |
---|
55 | error('COLS and TYPES have different lengths.') |
---|
56 | end |
---|
57 | if any(cellfun('isempty',cols)) |
---|
58 | error('COLS contains empty cells.') |
---|
59 | end |
---|
60 | if any(cellfun('isempty',types)) |
---|
61 | error('TYPES contains empty cells.') |
---|
62 | end |
---|
63 | s = ['create table ' table '(']; |
---|
64 | for k = 1:K |
---|
65 | s = [s cols{k} ' ' types{k} ', ']; |
---|
66 | end |
---|
67 | mym([s(1:length(s)-2) ')']) |
---|