阅读权限 255 威望 3 级论坛币 5819 个 通用积分 6540.6495 学术水平 579 点 热心指数 658 点 信用等级 519 点 经验 54928 点 帖子 993 精华 6 在线时间 1056 小时 注册时间 2013-1-7 最后登录 2022-4-7
已卖:1490 份资源
学科带头人
还不是VIP /贵宾
威望 3 级论坛币 5819 个 通用积分 6540.6495 学术水平 579 点 热心指数 658 点 信用等级 519 点 经验 54928 点 帖子 993 精华 6 在线时间 1056 小时 注册时间 2013-1-7 最后登录 2022-4-7
开心
2022-3-24 10:58:16
签到天数: 129 天
连续签到: 1 天
[LV.7]常住居民III
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
function output=SBinsert(conn,tartable,findata)
delete('D:/temp/*.csv')
cell2csv(['D:/temp/a.csv'],findata);
%%
sql=['load data local infile ''D:/temp/a.csv'' into table ', tartable ,' fields terminated by '','' optionally enclosed by ''"'' escaped by ''"'' lines terminated by ''\n'''];
exec(conn,sql);
end 复制代码 由于数据中有字符串,日期,和double格式 , 上传 一个cell用insert到数据库成龟速,试用fastinsert,datainsert无果后,无奈先把cell转成本地csv再用load data infile,速度还是可以的。命名为SBinsert
function cell2csv(fileName, cellArray, separator, excelYear, decimal)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(fileName, cellArray, separator, excelYear, decimal)
%
% fileName = Name of the file to save. [ i.e. 'text.csv' ]
% cellArray = Name of the Cell Array where the data is in
% separator = sign separating the values (default = ';')
% excelYear = depending on the Excel version, the cells are put into
% quotes before they are written to the file. The separator
% is set to semicolon (;)
% decimal = defines the decimal separator (default = '.')
%
% by Sylvain Fiedler, KA, 2004
% updated by Sylvain Fiedler, Metz, 06
% fixed the logical-bug, Kaiserslautern, 06/2008, S.Fiedler
% added the choice of decimal separator, 11/2010, S.Fiedler
%% Checking f黵 optional Variables
if ~exist('separator', 'var')
separator = ',';
end
if ~exist('excelYear', 'var')
excelYear = 1997;
end
if ~exist('decimal', 'var')
decimal = '.';
end
%% Setting separator for newer excelYears
if excelYear > 2000
separator = ';';
end
%% Write file
datei = fopen(fileName, 'w');
for z=1:size(cellArray, 1)
for s=1:size(cellArray, 2)
var = eval(['cellArray{z,s}']);
% If zero, then empty cell
if size(var, 1) == 0
var = '';
end
% If numeric -> String
if isnumeric(var)
var = num2str(var);
% Conversion of decimal separator (4 Europe & South America)
% http://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
if decimal ~= '.'
var = strrep(var, '.', decimal);
end
end
% If logical -> 'true' or 'false'
if islogical(var)
if var == 1
var = 'TRUE';
else
var = 'FALSE';
end
end
% If newer version of Excel -> Quotes 4 Strings
if excelYear > 2000
var = ['"' var '"'];
end
% OUTPUT value
fprintf(datei, '%s', var);
% OUTPUT separator
if s ~= size(cellArray, 2)
fprintf(datei, separator);
end
end
if z ~= size(cellArray, 1) % prevent a empty line at EOF
% OUTPUT newline
fprintf(datei, '\n');
end
end
% Closing file
fclose(datei);
% END 复制代码
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
总评分: 经验 + 30
论坛币 + 30
查看全部评分