从sql2008表中获取向量
Get a vector from a sql 2008 table
我在SQL server 2008:中有一个名为test的表,它有6列6行
1att 2att 3att 4att 5att 6att
----------------------------------
467 116 480 477 491 697
NULL 219 481 113 488 466
NULL NULL 477 466 455 480
NULL NULL NULL 527 483 629
NULL NULL NULL NULL 483 483
NULL NULL NULL NULL NULL 697
我想要一个有所有值的向量,但有列顺序。
所以我的矢量看起来像
[
467 116 480 477 491 697 //row 1
116 219 481 113 488 466 //row 2
480 481 477 466 455 480 //row 3
477 113 466 527 483 629 //row 4
491 488 455 483 483 483 //row 5
697 466 480 629 483 697 //row 6
]
要做到这一点,我使用Visual Studio 2008,使用c++,nd使用ADO进行连接。
// svd-conn.cpp: archivo de proyecto principal.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#define MSADO15_PATH "c:program filescommon filessystemadomsado15.dll"
#ifdef _MSC_VER
#import MSADO15_PATH rename ("EOF","adoEOF") no_namespace
#else
#define V_INT(X) V_UNION(X, intVal)
#define V_UINT(X) V_UNION(X, uintVal)
#include "msado15.tlh"
#endif
#include <comutil.h>
struct InitOle{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} InitOle_tag;
//------------------ utility fns to simplify access to recordset fields
_bstr_t RsItem( _RecordsetPtr p, BSTR fldName ){
// by field name
return( p->Fields->Item[_variant_t(fldName)]->Value );
}
_bstr_t RsItem( _RecordsetPtr p, long nIdx ){
// by field # (0 is first)
return( p->Fields->Item[_variant_t(nIdx)]->Value );
}
//-------------------------------- The Program ----------------
int main(){
_RecordsetPtr spRs;
HRESULT hr;
_bstr_t sConn= "driver={sql server};SERVER=VIRTUALPC;Database=test;UID=sa; PWD=;";
_bstr_t sSQL= "SELECT * FROM dbo.test ;";
try{
hr= spRs.CreateInstance( __uuidof(Recordset) );
if FAILED(hr)
printf("CreateInstance failedn");
else
printf("CreateInstance SUCCESSn");
hr= spRs->Open( sSQL, sConn, adOpenForwardOnly, adLockReadOnly, adCmdText );
if FAILED(hr)
printf("Open failedn");
else
printf("Open SUCCESSn");
printf("spRs->adoEOF %sn",spRs->adoEOF);
while( !(spRs->adoEOF) ) {
printf("%st%sn",(char*) RsItem( spRs, 0L ),(char*) RsItem( spRs, 1L ),(char*) RsItem( spRs, 2L ) );
spRs->MoveNext();
}
spRs->Close();
} catch( _com_error &e) {
printf("Error:%sn",(char*)e.Description());
}
return 0;
}
有了这段代码,在while循环中,我可以得到值,但我怎么能以正确的方式将它们插入向量中呢?
那么我该如何合并向量类:
vector <double> v;
有愿望的矢量吗??
我知道要插入矢量,我会做这个
v.push_back(467);
。。。。
但如何用程序化的方式来做,实际上NULLS才是真正的问题。。。
从问题来看:事实上,NULLS才是真正的问题
这确实是一个相对简单的问题。通过跟踪行和列,并且由于矢量是按行顺序填充的,因此可以从它们的转置位置获得对角线以下单元格的值,即矢量中易于获得的数据值
也许是以下内容:
#define COLS_PER_ROW 6
int rowNum = 0;
vector <double> v(COLS_PER_ROW * COLS_PER_ROW, 0);
while( !(spRs->adoEOF) ) {
for (colNum = 0; colNum < COLS_PER_ROW; colNum++)
{
double dVal;
bstr_t sVal = RsItem(spRs, colNum);
if (sVal) // this test is equivalent to if (colNum >= rowNum)
{
dval = strtod(sVal, NULL);
}
else
dVal = v[colNum * COLS_PER_ROW + rowNum]; // not an error as we want to read the transpose loc for the cell.
}
rowNum++;
spRs->MoveNext();
}
关于空值测试的小注释if (sVal)
测试数据库提供的值是否为null
更好的测试可能是if (colNum >= rowNum)
,即假设在下三角中找到零值,并且与我们在上三角中的"镜像"/"转置"位置获得的值一致。
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 从 exprTk 获取向量作为输出
- C++ 获取向量中常量字符* [ ] 的长度
- 如何使用模板获取向量中的数据类型?
- C++ 通过 std::transform 的 vector 索引获取向量
- C++ 运算符"="重载 - 在 lhs 上获取向量中的所有值,使其等于 rhs 上的双精度值
- 通过交互器获取向量中的索引
- 无法获取向量中元素的总和
- 获取向量中元素的地址
- 如何获取向量的每个第 N 个元素的"address"
- 我应该如何从特征 3 中的张量切片中获取向量
- 如何获取向量的排序索引
- 向量C ,获取向量的第二个元素有什么问题
- 获取向量<float>中 k 个最大值的 indize 的有效方法
- 获取向量的指定元素
- 如何在具有相同基类的两个不同类中获取向量
- 如何获取向量的这些元素,其中另一个向量在相应的索引处具有'1
- 一个模板函数,用于获取向量中的最大值及其相应的索引
- 获取向量 c++ 中的字符串计数
- C++linux获取向量迭代器不兼容