使用 MATLAB 从 mySQL 数据库检索 blob 字段

发布于 2024-08-29 12:32:14 字数 1414 浏览 3 评论 0原文

我正在使用 JDBC 和 mySQL java 连接器 访问公共 mySQL 数据库。 exonCount 为 int(10),exonStarts 和 exonEnds 为 longblob 字段。

javaaddpath('mysql-connector-java-5.1.12-bin.jar')
host = 'genome-mysql.cse.ucsc.edu';
user = 'genome';
password = '';
dbName = 'hg18'; 
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';
dbConn = database(dbName, user , password, jdbcDriver, jdbcString);
gene.Symb = 'CDKN2B';
% Check to make sure that we successfully connected
if isconnection(dbConn)
    qry = sprintf('SELECT exonCount, exonStarts, exonEnds FROM refFlat WHERE geneName=''%s''',gene.Symb);
    result = get(fetch(exec(dbConn, qry)), 'Data');
    fprintf('Connection failed: %s\n', dbConn.Message);
end

结果如下:

result = 
    [2]    [18x1 int8]    [18x1 int8]
    [2]    [18x1 int8]    [18x1 int8]

result{1,2}'
ans =
   50   49   57   57   50   57   48   49   44   50   49   57   57   56   54   55   51   44

这是错误的。第二列和第三列中向量的长度应与第一列中的数字匹配。

例如,第一个 blob 应该是 [21992901; 21998673]。我怎样才能转换它?


更新:

提交此问题后,我认为它可能是字符串的十六进制表示形式。 已经确认:

>> char(result{1,2}')
ans =
21992901,21998673,

所以现在我需要将所有 blob 十六进制数据转换为数字向量。仍在考虑以矢量化方式进行,因为行数可能很大。

I'm accessing public mySQL database using JDBC and mySQL java connector. exonCount is int(10), exonStarts and exonEnds are longblob fields.

javaaddpath('mysql-connector-java-5.1.12-bin.jar')
host = 'genome-mysql.cse.ucsc.edu';
user = 'genome';
password = '';
dbName = 'hg18'; 
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';
dbConn = database(dbName, user , password, jdbcDriver, jdbcString);
gene.Symb = 'CDKN2B';
% Check to make sure that we successfully connected
if isconnection(dbConn)
    qry = sprintf('SELECT exonCount, exonStarts, exonEnds FROM refFlat WHERE geneName=''%s''',gene.Symb);
    result = get(fetch(exec(dbConn, qry)), 'Data');
    fprintf('Connection failed: %s\n', dbConn.Message);
end

Here is the result:

result = 
    [2]    [18x1 int8]    [18x1 int8]
    [2]    [18x1 int8]    [18x1 int8]

result{1,2}'
ans =
   50   49   57   57   50   57   48   49   44   50   49   57   57   56   54   55   51   44

This is wrong. The length of vectors in 2nd and 3rd columns should match the numbers in the 1st column.

The 1st blob, for example, should be [21992901; 21998673]. How I can convert it?


Update:

Just after submitting this question I thought it might be hex representation of a string.
And it was confirmed:

>> char(result{1,2}')
ans =
21992901,21998673,

So now I need to convert all blobs hex data into numeric vectors. Still thinking to do it in a vectorized way, since number of rows can be large.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琉璃梦幻 2024-09-05 12:32:14

这会将结果中除第一列数据之外的所有字符数据转换为数值向量,并将结果放回适当的单元格中:

result(:,2:end) = cellfun(@(x) str2num(char(x'))',...  %# Apply fcn to each cell
                          result(:,2:end),...          %# Input cells
                          'UniformOutput',false);      %# Output as a cell array

This will convert your character data to numeric vectors for all except the first column of data in result, placing the results back into the appropriate cells:

result(:,2:end) = cellfun(@(x) str2num(char(x'))',...  %# Apply fcn to each cell
                          result(:,2:end),...          %# Input cells
                          'UniformOutput',false);      %# Output as a cell array
赤濁 2024-09-05 12:32:14

我建议使用 textscan

exons = cellfun(@(x) textscan(char(x'),'%d','Delimiter',','),...
result(:,2:end),'UniformOutput',false);

要获取两个数字中每一个的元胞数组,您可以将格式字符串替换为 %d,%d 并删除 Delimiter 选项。

I suggest using textscan

exons = cellfun(@(x) textscan(char(x'),'%d','Delimiter',','),...
result(:,2:end),'UniformOutput',false);

To get a cell array for each of the two numbers, you can replace the format string by %d,%d and drop the Delimiter option.

十级心震 2024-09-05 12:32:14

这就是我所做的:

function res = blob2num(x)
res = str2double(regexp(char(x'),'[^,]+','match')');

那么

exons = cellfun(@blob2num,result(:,2:3)','UniformOutput',0)
exons = 
    [2x1 double]    [2x1 double]
    [2x1 double]    [2x1 double]

还有更好的解决方案吗?可能正在检索数据?

Here is what I do:

function res = blob2num(x)
res = str2double(regexp(char(x'),'[^,]+','match')');

then

exons = cellfun(@blob2num,result(:,2:3)','UniformOutput',0)
exons = 
    [2x1 double]    [2x1 double]
    [2x1 double]    [2x1 double]

Any better solution? May be on the step of retrieving data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文