matlab-VQB操作MYSQL数据库及中文乱码解决

首先安装好ODBC MYSQL驱动

(64位)

http://cdn.mysql.com/Downloads/Connector-ODBC/5.2/mysql-connector-odbc-5.2.2-winx64.msi

(32位)

http://cdn.mysql.com/Downloads/Connector-ODBC/5.2/mysql-connector-odbc-5.2.2-win32.msi

然后,打开vqb

查询


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 

插入

 


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 

MATLAB通过ODBC操作MYSQL的中文乱码,需要在数据源中进行配置


matlab-VQB操作MYSQL数据库及中文乱码解决
 

查询数据(中文乱码消失了)


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 插入中文数据

>> y2={'追求'}

y2 =

    '追求'

>>


matlab-VQB操作MYSQL数据库及中文乱码解决
 测试一下,查看中文数据是否正确插入


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 

此外,关于字符编码转换的2个函数

native2unicode

Convert numeric bytes to Unicode characters

Syntax

unicodestr = native2unicode(bytes)
unicodestr = native2unicode(bytes, encoding)

Description

unicodestr = native2unicode(bytes) takes a vector containing numeric values in the range [0,255] and converts these values as a stream of 8-bit bytes to Unicode characters. The stream of bytes is assumed to be in the MATLAB default character encoding scheme. Return value unicodestr is a char vector that has the same general array shape as bytes.

unicodestr = native2unicode(bytes, encoding) does the conversion with the assumption that the byte stream is in the character encoding scheme specified by the string encoding. encoding must be the empty string ('') or a name or alias for an encoding scheme. Some examples are 'UTF-8', 'latin1', 'US-ASCII', and 'Shift_JIS'. For common names and aliases, see the Web site http://www.iana.org/assignments/character-sets. If encoding is unspecified or is the empty string (''), the MATLAB default encoding scheme is used.

Note   If bytes is a char vector, it is returned unchanged.

Examples

This example begins with a vector of bytes in an unknown character encoding scheme. The user-written function detect_encoding determines the encoding scheme. If successful, it returns the encoding scheme name or alias as a string. If unsuccessful, it throws an error represented by an MException object, ME. The example calls native2unicode to convert the bytes to Unicode characters:

try
   enc = detect_encoding(bytes);
	   str = native2unicode(bytes, enc);
	   disp(str);
catch ME
	   rethrow(ME);
end

Note that the computer must be configured to display text in a language represented by the detected encoding scheme for the output of disp(str) to be correct.

 

================

unicode2native

Convert Unicode characters to numeric bytes

Syntax

bytes = unicode2native(unicodestr)
bytes = unicode2native(unicodestr, encoding)

Description

bytes = unicode2native(unicodestr) takes a char vector of Unicode characters, unicodestr, converts it to the MATLAB default character encoding scheme, and returns the bytes as a uint8 vector, bytes. Output vector bytes has the same general array shape as the unicodestr input. You can save the output of unicode2native to a file using the fwrite function.

bytes = unicode2native(unicodestr, encoding) converts the Unicode characters to the character encoding scheme specified by the string encoding. encoding must be the empty string ('') or a name or alias for an encoding scheme. Some examples are 'UTF-8', 'latin1', 'US-ASCII', and 'Shift_JIS'. For common names and aliases, see the Web site http://www.iana.org/assignments/character-sets. If encoding is unspecified or is the empty string (''), the MATLAB default encoding scheme is used.

Examples

This example begins with two strings containing Unicode characters. It assumes that string str1 contains text in a Western European language and string str2 contains Japanese text. The example writes both strings into the same file, using the ISO-8859-1 character encoding scheme for the first string and the Shift-JIS encoding scheme for the second string. The example uses unicode2native to convert the two strings to the appropriate encoding schemes.

fid = fopen('mixed.txt', 'w');
bytes1 = unicode2native(str1, 'ISO-8859-1');
fwrite(fid, bytes1, 'uint8');
bytes2 = unicode2native(str2, 'Shift_JIS');
fwrite(fid, bytes2, 'uint8');
fclose(fid);

 

 

 

  

 

 

报表输出

Display=>report


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 

是否显示表头设置

display=>report generator

在这选择databaselbx.rpt

然后选择table-ans,可看到表头选项


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 通过这种方式输出没有表头,我们修改y3变量,人为加上表头

>> y3=[{'名称'};y3]

y3 =

    '名称'
    '上大'
    '追求'

>>


matlab-VQB操作MYSQL数据库及中文乱码解决
 

 

逻辑型数

 


matlab-VQB操作MYSQL数据库及中文乱码解决
 

>> z2

z2 =

    '上大'    [1]
    '追求'    [0]

>>

 

逻辑型变量的输出形式如下:

>> insert(myconn,'myst',colnames,{'张三',logical(1)})