MySQL二进制协议准备响应

时间:2015-07-08 18:09:40

标签: mysql database binary-data data-transfer

处理一些使用MySQL二进制协议与数据库通信的代码。

http://dev.mysql.com/doc/internals/en/client-server-protocol.html

我有一张桌子:

CREATE TABLE People ( ID  INTEGER, Name VARCHAR(64), Age  SMALLINT, Sex  CHAR(1), Height DOUBLE);

当我使用语句"SELECT * FROM People where sex=? or Age=?"向服务器发送COM_STMT_PREPARE时。我从服务器返回COM_STMT_PREPARE_OK

此软件包包含 2 个参数和 5 列,所有这些列都在ColumnDefinition41个数据包中定义,并具有以下值:

PARAMETERS:
ColumnDefinition:
    catalog:                    def
    schema:                     <empty string>
    table:                      <empty string>
    orgTable:                   <empty string>
    name:                       ?
    orgName:                    <empty string>
    lengthOfFixedField:         12
    charSet:                    63
    columnLength:               0
    type:                       253(MYSQL_TYPE_VAR_STRING)
    flags:                      128
    decimal:                    0

ColumnDefinition:
    catalog:                    def
    schema:                     <empty string>
    table:                      <empty string>
    orgTable:                   <empty string>
    name:                       ?
    orgName:                    <empty string>
    lengthOfFixedField:         12
    charSet:                    63
    columnLength:               0
    type:                       253(MYSQL_TYPE_VAR_STRING)
    flags:                      128
    decimal:                    0


Columns:
ColumnDefinition:
    catalog:                    def
    schema:                     test
    table:                      People
    orgTable:                   people
    name:                       ID
    orgName:                    ID
    lengthOfFixedField:         12
    charSet:                    63
    columnLength:               11
    type:                       3(MYSQL_TYPE_LONG)
    flags:                      0
    decimal:                    0

ColumnDefinition:
    catalog:                    def
    schema:                     test
    table:                      People
    orgTable:                   people
    name:                       Name
    orgName:                    Name
    lengthOfFixedField:         12
    charSet:                    33
    columnLength:               192
    type:                       253(MYSQL_TYPE_VAR_STRING)
    flags:                      0
    decimal:                    0

ColumnDefinition:
    catalog:                    def
    schema:                     test
    table:                      People
    orgTable:                   people
    name:                       Age
    orgName:                    Age
    lengthOfFixedField:         12
    charSet:                    63
    columnLength:               6
    type:                       2(MYSQL_TYPE_SHORT)
    flags:                      0
    decimal:                    0

ColumnDefinition:
    catalog:                    def
    schema:                     test
    table:                      People
    orgTable:                   people
    name:                       Sex
    orgName:                    Sex
    lengthOfFixedField:         12
    charSet:                    33
    columnLength:               3
    type:                       254(MYSQL_TYPE_STRING)
    flags:                      0
    decimal:                    0

ColumnDefinition:
    catalog:                    def
    schema:                     test
    table:                      People
    orgTable:                   people
    name:                       Height
    orgName:                    Height
    lengthOfFixedField:         12
    charSet:                    63
    columnLength:               22
    type:                       5(MYSQL_TYPE_DOUBLE)
    flags:                      0
    decimal:                    31

第二个参数的类型为MYSQL_TYPE_VAR_STRING,但我希望其类型为MYSQL_TYPE_SHORT,因为它绑定到表Age中的列People有这种类型。

参数总是具有类型MYSQL_TYPE_VAR_STRING,还是有某种方法让服务器响应我绑定的参数类型?

如果值得注意:

> mysql --help
mysql  Ver 14.14 Distrib 5.6.22, for osx10.10 (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

1 个答案:

答案 0 :(得分:0)

通过二进制协议传递给SQL Server的参数始终编码为字符串。然后,SQL服务器解析字符串并将其内部转换为正确的类型。