使用 MATLAB 从 mySQL 数据库检索 blob 字段
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会将
结果
中除第一列数据之外的所有字符数据转换为数值向量,并将结果放回适当的单元格中: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:我建议使用 textscan
要获取两个数字中每一个的元胞数组,您可以将格式字符串替换为
%d,%d
并删除Delimiter
选项。I suggest using textscan
To get a cell array for each of the two numbers, you can replace the format string by
%d,%d
and drop theDelimiter
option.这就是我所做的:
那么
还有更好的解决方案吗?可能正在检索数据?
Here is what I do:
then
Any better solution? May be on the step of retrieving data?