ODBC Driver提供ODBC接口供用户访问实时数据库数据,ODBC接口遵循微软ODBC规范.
安装实时数据库客户端,安装包中包含了iHyperDB ODBC Driver.
1.ODBC数据源配置
通过操作系统控制面板->管理工具->数据源打开数据源管理器。
注意:Windows 64位系统需通过32位数据源管理器管理,一般路径C:\Windows\SysWOW64\odbcad32.exe
1.1 新增系统/用户数据源
选择系统DSN或用户DSN,点击添加按扭,弹出创建新数据源界面
选择iHyperDB ODBC Driver,点击完成。弹出iHyperDB ODBC数据源配置界面。
输入数据源名称、数据源描述、实时数据库的IP地址、端口号、用户名、密码等信息,并指定连接超时和查询超时时间,点击确定按扭,完成数据源新增。
数据源名称:长度最长为31个字符,不能为空;
数据源描述:长度最长为1023个字符;
主机Ip地址:长度最长为16 不能为空;
备机Ip地址:长度最长为16 不能为空;
用户名:长度最长为31 不能为空 这个为实时数据库用户名;
密码:长度最长为63,最短为5,支持字符,数字,以及0-9数字键上对应的特殊字符。
连接超时范围为0-32767
查询超时范围为0-32767
1.2 修改系统/用户数据源
ODBC数据源管理器中选择数据源,点击“配置”按扭,弹出iHyperDB ODBC 数据源配置界面修改系统、用户数据源,修改完成后点击“确定”按钮保存,点击“取消”按钮,取消修改。
1.3 删除系统/用户数据源
ODBC数据源管理器中选择数据源,点击“删除”按扭,删除数据源。
2. HDSQL
HDSQL遵循SQL语法实现,用户可根据HDSQL定义的关系表及SQL语法,编写SQL语句,查询实时数据库数据。
2.1 TABLE
HDSQL支持如下表的Select查询
2.1.1 HDARCH
FTag |
CHAR(128) |
Tag名称 |
FTime |
CHAR(24) |
记录时间戳 |
FQuality |
Uint16 |
记录质量 |
FIValue |
Int32 |
整形值 |
FSValue |
CHAR(1000) |
字符或BLOB值 |
FFValue |
Float64 |
浮点值 |
2.1.2 HDINTER
FTag |
CHAR(128) |
Tag名称 |
FTime |
CHAR(24) |
记录时间戳 |
FQuality |
Uint16 |
记录质量 |
FIValue |
Int32 |
整形值 |
FSValue |
CHAR(1000) |
字符或BLOB值 |
FFValue |
Float64 |
浮点值 |
FTimeStep |
CHAR(24) |
插值间隔 |
FInterType |
CHAR(24) |
插值类型 |
2.2 HDSQL语法说明
(select_statement) ::= SELECT (select_list) FROM (table_source) WHERE (condition) [LIMIT (Integer) ASC|DESC]
(select_list) ::=(column_alias) [, (column_alias)]| buildin_func(fivalue|ffvalue)[, buildin_func(fivalue|ffvalue) ]| *
(condition) ::= (compare_expr) [and|or (compare_expr)]
(compare_expr)::= [FTime (Comparison) (time string)]|[FTag = (tag name string)]|[FTimeStep= time_interval]|[FInterType= LINE|PREV]
(Compariosn):: =|>|<|>=|<=
(table_source):= HDArch|HDInter
(time_ interval):= Number(time_mode)
(time_mode): D|H|M|S|MS
(time string):= ‘YYY-MM-DD hh:mm:ss.xxx’
(buildin_func):max|min|count|average|sum
关键字SELECT、FROM、WHERE、LIMIT、ASC、DESC大小写不敏感
▪Table_source仅支持HDarch(历史归档表)和HDInter(历史插值表),表名、字段名大小写不敏感
▪可以select一个表中的多个列或所有列(用*表示),不支持多表select
▪Where可使用And和or组合多个比较表达式,And与or大小写不敏感
▪比较表达式仅支持FTag、FTimestep、FInterType的‘=’比较操作及Time字段的=、>、<、>=、<=、比较操作
▪比较表达式左边为字段名,右边为比较值
▪可通过LIMIT限制最大查询条数,Limit为int32类型,若输入超过2147483647的值,会被截断,输入负数会报错
▪一个点的数据默认按时间从大到小排序(DESC),可通过ASC指定为从小到大排序
▪查询HDInter表时,可以在Where中指定FTimeStep间隔,其中time_interval可表示为:时间间隔长度+时间模式,如10M,表示间隔10分钟。D表示日(1-24855),H表示小时(1-596523),M表示分钟(1-35791394),S表示秒(1-2147483647),MS(1-2147483647)表示毫秒,大小写不敏感
▪SQL最大长度255
▪若未指定插值类型,默认为LINE
▪若未指定插值 间隔,默认为60秒
▪ 内置函数,时间区间不支持[timeA, timeA],多个时间区间之间不可以有交集
▪select 中若出现内置函数,不允许再select其它字段,如出现select ftag, MAX(FIVALUE)。会报解析失败
示例:
1.Select * from hdarch where ftag='abc' and ftime < '2013-09-18 08:00:00.000’ and ftime > '2013-09-18 09:00:00.000' limit 10 asc
查询归档表,Tag名为abc的点,2013年9月18日 8点到9点的数据,记录数限制最多10条,按时间升序排序。
2.Select * from hdinter where ftag='abc' and ftime < '2013-09-18 08:00:00.000’ and ftime > '2013-09-18 09:00:00.000' and ftimestep = 10s and fintertype = line limit 10 asc
查询插值表,Tag名为abc的点,2013年9月18日 8点到9点的数据,插值间隔为10s,插值类型为线性插值,记录数限制最多10条, 按时间升序排序。
3.Select max(fivalue),min(fivalue), count(fivalue) from hdarch where ftag='abc1' and ftime > '2014-01-01 08:00:00.000' and ftime < '2014-01-02 08:00:00.000';
计算归档表2014-01-01 08:00:00.000至2014-01-02 08:00:00.000间的最大记录值和最小记录值
4.Select max(fivalue),min(fivalue),count(fivalue) from hdinter where ftag='abc1' and ftime > '2014-01-01 08:00:00.000' and ftime < '2014-01-02 08:00:00.000' AND FTIMESTEP = 1s AND FINTERTYPE = line;
计算插值表2014-01-01 08:00:00.000至2014-01-02 08:00:00.000间的最大记录值和最小记录值,插值方式为线性插值,时间间 隔为1秒
3 HDODBC接口
3.1 支持的ODBC接口
●SQLRETURN SQL_API SQLAllocHandle(SQLSMALLINT HandleType,SQLHANDLE InputHandle, SQLHANDLE *OutputHandle);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms712455(v=vs.85).aspx
●SQLRETURN SQL_API SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms710123(v=vs.85).aspx
●SQLRETURN SQL_API SQLConnect(SQLHDBC ConnectionHandle,
▪SQLCHAR *ServerName, SQLSMALLINT NameLength1,
▪SQLCHAR *UserName, SQLSMALLINT NameLength2,
▪SQLCHAR *Authentication, SQLSMALLINT NameLength3);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms711810(v=vs.85).aspx
●SQLRETURN SQLDisconnect( SQLHDBC ConnectionHandle);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms713946(v=vs.85).aspx
●SQLRETURN SQL_API SQLSetConnectAttr(SQLHDBC ConnectionHandle,
SQLINTEGER Attribute, SQLPOINTER Value,
SQLINTEGER StringLength);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms713605%28v=vs.85%29.aspx
●SQLRETURN SQL_API SQLExecDirect
(
SQLHSTMT StatementHandle,
__in_ecount_opt(TextLength) SQLCHAR* StatementText,
SQLINTEGER TextLength
);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms713611(v=vs.85).aspx
●SQLRETURN SQL_API SQLFetch(SQLHSTMT StatementHandle);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms712424(v=vs.85).aspx
●SQLRETURN SQL_API SQLBindCol(SQLHSTMT StatementHandle,
SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType,
SQLPOINTER TargetValue, SQLLEN BufferLength,
SQLLEN *StrLen_or_Ind);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms711010(v=vs.85).aspx
●SQLRETURN SQL_API SQLDataSources(SQLHENV EnvironmentHandle,
SQLUSMALLINT Direction, SQLCHAR *ServerName,
SQLSMALLINT BufferLength1, SQLSMALLINT *NameLength1,
SQLCHAR *Description, SQLSMALLINT BufferLength2,
SQLSMALLINT *NameLength2);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms711004(v=vs.85).aspx
●SQLRETURN SQLGetInfo(
SQLHDBC ConnectionHandle,
SQLUSMALLINT InfoType,
SQLPOINTER InfoValuePtr,
SQLSMALLINT BufferLength,
SQLSMALLINT * StringLengthPtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms711681(v=vs.85).aspx
●SQLRETURN SQLSetEnvAttr(
SQLHENV EnvironmentHandle,
SQLINTEGER Attribute,
SQLPOINTER ValuePtr,
SQLINTEGER StringLength);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms709285(v=vs.85).aspx
●SQLRETURN SQLSetStmtAttr(
SQLHSTMT StatementHandle,
SQLINTEGER Attribute,
SQLPOINTER ValuePtr,
SQLINTEGER StringLength);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms712631(v=vs.85).aspx
●SQLRETURN SQLRowCount(
SQLHSTMT StatementHandle,
SQLLEN * RowCountPtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms711835(v=vs.85).aspx
●SQLRETURN SQLNumResultCols(
SQLHSTMT StatementHandle,
SQLSMALLINT * ColumnCountPtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms715393(v=vs.85).aspx
●SQLRETURN SQLDriverConnect(
SQLHDBC ConnectionHandle,
SQLHWND WindowHandle,
SQLCHAR * InConnectionString,
SQLSMALLINT StringLength1,
SQLCHAR * OutConnectionString,
SQLSMALLINT BufferLength,
SQLSMALLINT * StringLength2Ptr,
SQLUSMALLINT DriverCompletion);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms715433(v=vs.85).aspx
●SQLRETURN SQLGetDiagRec(
SQLSMALLINT HandleType,
SQLHANDLE Handle,
SQLSMALLINT RecNumber,
SQLCHAR * SQLState,
SQLINTEGER * NativeErrorPtr,
SQLCHAR * MessageText,
SQLSMALLINT BufferLength,
SQLSMALLINT * TextLengthPtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms716256(v=vs.85).aspx
●SQLRETURN SQLGetDiagField(
SQLSMALLINT HandleType,
SQLHANDLE Handle,
SQLSMALLINT RecNumber,
SQLSMALLINT DiagIdentifier,
SQLPOINTER DiagInfoPtr,
SQLSMALLINT BufferLength,
SQLSMALLINT * StringLengthPtr);
接口详细说明参见:http://msdn.microsoft.com/en-us/library/ms710181(v=vs.85).aspx
●SQLRETURN SQLGetConnectAttr(
SQLHDBC ConnectionHandle,
SQLINTEGER Attribute,
SQLPOINTER ValuePtr,
SQLINTEGER BufferLength,
SQLINTEGER * StringLengthPtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms710297(v=vs.85).aspx
●SQLRETURN SQLExtendedFetch(
SQLHSTMT StatementHandle,
SQLUSMALLINT FetchOrientation,
SQLLEN FetchOffset,
SQLULEN * RowCountPtr,
SQLUSMALLINT * RowStatusArray);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms713591(v=vs.85).aspx
●SQLRETURN SQLColAttribute (
SQLHSTMT StatementHandle,
SQLUSMALLINT ColumnNumber,
SQLUSMALLINT FieldIdentifier,
SQLPOINTER CharacterAttributePtr,
SQLSMALLINT BufferLength,
SQLSMALLINT * StringLengthPtr,
SQLLEN * NumericAttributePtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms713558(v=vs.85).aspx
●SQLRETURN SQLFetchScroll(
SQLHSTMT StatementHandle,
SQLSMALLINT FetchOrientation,
SQLLEN FetchOffset);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms714682(v=vs.85).aspx
●SQLRETURN SQLGetData(
SQLHSTMT StatementHandle,
SQLUSMALLINT Col_or_Param_Num,
SQLSMALLINT TargetType,
SQLPOINTER TargetValuePtr,
SQLLEN BufferLength,
SQLLEN * StrLen_or_IndPtr);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms715441(v=vs.85).aspx
●SQLRETURN SQLMoreResults(
SQLHSTMT StatementHandle);
接口详细说明参见http://msdn.microsoft.com/en-us/library/ms714673(v=vs.85).aspx
3.2 Example
3.2.1 C/C++
example
#include "sql.h"
#include <sqlext.h>
int32 main(int argc, char** argv)
{
SQLHANDLE hEnv = SQL_NULL_HENV;
SQLHANDLE hDbc = SQL_NULL_HDBC;
SQLHANDLE hStmt = SQL_NULL_HSTMT;
SQLRETURN nRet = SQL_SUCCESS;
HDBC* pHDDbc = NULL;
int8 nTimeOut = 3;
char szSQL[1024];
char szTagName[128];
char szTimeStr[24];
uint16 nQuality;
int32 nValue;
char szValue[1024];
float64 fValue;
char szTimeStep[24];
char szInterType[24];
int32 nCount = 0;
// 申请HDODBC环境句柄
nRet = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &hEnv);
if(SQL_SUCCESS != nRet)
{
return nRet;
}
// 申请HDODBC连接句柄
nRet = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 设置HDODBC连接超时
nRet = SQLSetConnectAttr(hDbc, SQL_ATTR_CONNECTION_TIMEOUT, &nTimeOut, sizeof(nTimeOut));
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 连接数据源abc
nRet = SQLConnect(hDbc, (SQLCHAR*)"abc", strlen("abc") + 1, NULL, 0, NULL , 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 申请Stmt句柄
nRet = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 执行SQL
strcpy(szSQL, "select * from HDInter where Ftag=\'TAG\' and FTime >= \'2013-09-12 10:00:00.000\' and FTime <= \'2013-09-18 11:00:00.000\' LIMIT 10 DESC");
nRet = SQLExecDirect(hStmt, (SQLCHAR*)szSQL, strlen(szSQL) + 1);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 0, SQL_CHAR, szTagName, 128, 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 1, SQL_CHAR, szTimeStr, 128, 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 2, SQL_INTEGER, &nQuality, sizeof(nQuality), 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 3, SQL_INTEGER, &nValue, sizeof(nValue), 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 4, SQL_CHAR,szValue, sizeof(szValue), 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 5, SQL_FLOAT,&fValue, sizeof(fValue), 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 6, SQL_CHAR,szTimeStep, sizeof(szTimeStep), 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 绑定字段buffer
nRet = SQLBindCol(hStmt, 7, SQL_CHAR,szInterType, sizeof(szInterType), 0);
if(SQL_SUCCESS != nRet)
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return nRet;
}
// 逐条取查询结果并printf
while(SQL_SUCCESS == SQLFetch(hStmt))
{
printf("Tag=%s, Time=%s,Quality=%d,FIValue=%d,FSValue=%s,FFValue=%f, FTimeStep=%s,FinterType=%s\n",
szTagName, szTimeStr, nQuality,nValue,szValue,fValue, szTimeStep,szInterType);
}
// 断开连接
SQLDisconnect(hDbc);
// 释放句柄
SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
return 1;
}
3.2.1 C#
example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
namespace baosight.ihyperdb.csharp.example.odbc
{
class Program
{
static void Main(string[] args)
{
try
{
// 连接字符串,支持三种格式
string constr = "dsn=abc;";
//string constr = "dsn=abc;Uid=admin;Pwd=admin";
//string constr = "DRIVER={iHyperDB ODBC Driver};SERVER=127.0.0.1;PORT=5678;UID=admin;PWD=admin";
// 建立连接
OdbcConnection conn = new OdbcConnection(constr);
conn.Open();
// 创建SQL命令
OdbcCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from hdinter where ftag='hdsys_cpu' and ftime < '2014-04-16 12:00:00.000' and ftime > '2014-04-16 9:00:00.000' and ftimestep = 1S limit 10 DESC;";
// 执行命令
OdbcDataReader reader = cmd.ExecuteReader();
// 读取结果集
Boolean bOk = reader.Read();
while (bOk)
{
// 读取当前结果集字段值
string strName = reader.GetString(0);
string strTime = reader.GetString(1);
Int32 nQuality = reader.GetInt16(2);
Int32 nValue = reader.GetInt32(3);
string strValue = reader.GetString(4);
double fValue = reader.GetDouble(5);
Console.Write("name:" + strName.ToString() + " time:" + strTime.ToString()+"\n");
bOk = reader.Read();
}
reader.Close();
cmd.Dispose();
conn.Close();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}