请选择 进入手机版 | 继续访问电脑版
楼主: slrosssss
1777 1

[程序化交易] 关于matlab上传数据库问题 [推广有奖]

只想做个安静的逗逼

学科带头人

59%

还不是VIP/贵宾

-

威望
3
论坛币
5806 个
通用积分
6536.1541
学术水平
579 点
热心指数
658 点
信用等级
519 点
经验
54828 点
帖子
993
精华
6
在线时间
1056 小时
注册时间
2013-1-7
最后登录
2022-4-7

初级热心勋章 初级学术勋章 初级信用勋章 中级热心勋章 中级信用勋章 高级热心勋章 中级学术勋章 高级学术勋章

slrosssss 在职认证  企业认证  发表于 2015-1-28 14:42:12 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. function output=SBinsert(conn,tartable,findata)
  2. delete('D:/temp/*.csv')
  3. cell2csv(['D:/temp/a.csv'],findata);
  4. %%
  5. sql=['load data local infile ''D:/temp/a.csv'' into table ', tartable ,'  fields terminated by '',''  optionally enclosed by ''"'' escaped by ''"''   lines terminated by ''\n'''];
  6. exec(conn,sql);
  7. end
复制代码
由于数据中有字符串,日期,和double格式 ,上传一个cell用insert到数据库成龟速,试用fastinsert,datainsert无果后,无奈先把cell转成本地csv再用load data infile,速度还是可以的。命名为SBinsert

  1. function cell2csv(fileName, cellArray, separator, excelYear, decimal)
  2. % Writes cell array content into a *.csv file.
  3. %
  4. % CELL2CSV(fileName, cellArray, separator, excelYear, decimal)
  5. %
  6. % fileName     = Name of the file to save. [ i.e. 'text.csv' ]
  7. % cellArray    = Name of the Cell Array where the data is in
  8. % separator    = sign separating the values (default = ';')
  9. % excelYear    = depending on the Excel version, the cells are put into
  10. %                quotes before they are written to the file. The separator
  11. %                is set to semicolon (;)
  12. % decimal      = defines the decimal separator (default = '.')
  13. %
  14. %         by Sylvain Fiedler, KA, 2004
  15. % updated by Sylvain Fiedler, Metz, 06
  16. % fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
  17. % added the choice of decimal separator, 11/2010, S.Fiedler

  18. %% Checking f黵 optional Variables
  19. if ~exist('separator', 'var')
  20.     separator = ',';
  21. end

  22. if ~exist('excelYear', 'var')
  23.     excelYear = 1997;
  24. end

  25. if ~exist('decimal', 'var')
  26.     decimal = '.';
  27. end

  28. %% Setting separator for newer excelYears
  29. if excelYear > 2000
  30.     separator = ';';
  31. end

  32. %% Write file
  33. datei = fopen(fileName, 'w');

  34. for z=1:size(cellArray, 1)
  35.     for s=1:size(cellArray, 2)
  36.         
  37.         var = eval(['cellArray{z,s}']);
  38.         % If zero, then empty cell
  39.         if size(var, 1) == 0
  40.             var = '';
  41.         end
  42.         % If numeric -> String
  43.         if isnumeric(var)
  44.             var = num2str(var);
  45.             % Conversion of decimal separator (4 Europe & South America)
  46.             % http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
  47.             if decimal ~= '.'
  48.                 var = strrep(var, '.', decimal);
  49.             end
  50.         end
  51.         % If logical -> 'true' or 'false'
  52.         if islogical(var)
  53.             if var == 1
  54.                 var = 'TRUE';
  55.             else
  56.                 var = 'FALSE';
  57.             end
  58.         end
  59.         % If newer version of Excel -> Quotes 4 Strings
  60.         if excelYear > 2000
  61.             var = ['"' var '"'];
  62.         end
  63.         
  64.         % OUTPUT value
  65.         fprintf(datei, '%s', var);
  66.         
  67.         % OUTPUT separator
  68.         if s ~= size(cellArray, 2)
  69.             fprintf(datei, separator);
  70.         end
  71.     end
  72.     if z ~= size(cellArray, 1) % prevent a empty line at EOF
  73.         % OUTPUT newline
  74.         fprintf(datei, '\n');
  75.     end
  76. end
  77. % Closing file
  78. fclose(datei);
  79. % END
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:MATLAB matla atlab 上传数据 Mat matlab 数据库

已有 1 人评分经验 论坛币 收起 理由
fantuanxiaot + 30 + 30 精彩帖子

总评分: 经验 + 30  论坛币 + 30   查看全部评分

只想做个安静的逗逼
fantuanxiaot 发表于 2015-1-28 14:45:04 |显示全部楼层 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jr
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 14:02