正在编写一些使用 MySQL 二进制协议(protocol)与数据库通信的代码。
http://dev.mysql.com/doc/internals/en/client-server-protocol.html
我有一张 table :
CREATE TABLE People ( ID INTEGER, Name VARCHAR(64), Age SMALLINT, Sex CHAR(1), Height DOUBLE);
当我发送COM_STMT_PREPARE
时使用语句 “SELECT * FROM People where sex=? or Age=?”
发送到服务器。我得到一个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
,因为它绑定(bind)到列 Age
在具有此类型的表 People
中。
参数是否始终具有MYSQL_TYPE_VAR_STRING
类型,或者是否有某种方法可以让服务器以我绑定(bind)的参数类型进行响应?
如果值得注意:
> 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.
最佳答案
通过二进制协议(protocol)传递到 SQL Server 的参数始终编码为字符串。然后,SQL 服务器解析字符串并将它们在内部转换为正确的类型。
关于MySQL 二进制协议(protocol)准备响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31300414/