改写策略为王TSKING (源代码) 增加 SQL存储三 创建数据库表
//编写了几个连接数据库的函数,由于是公共函数,可以独立编写在新的文件中,例如我自己建了一个StkFunction.h ,StkFunction.cpp上
在CStkLibWnd 中增加了数据库相关的变量及初始化的函数,
BOOL m_IsSqlDBConnect; //是否已经连接Sql Db
_ConnectionPtr m_pCnn; //连接数据库
CSPString m_szConnString;//连接字符串
DBInitTable() ; //数据库初始化
CnnExecuteSql(CSPString sSql, bool nErr); //执行Sql命令
////////用于执行Sql命令
int CStkLibWnd::CnnExecuteSql(CSPString sSql, bool nErr)
{
_variant_t vntRecs[100];
long lRecs=1;
if(nErr)
{
try
{
m_pCnn->Execute(_bstr_t(sSql),
vntRecs,
adCmdText);
}
catch(_com_error e)
{
lRecs=0;
}
}
else
{
m_pCnn->Execute(_bstr_t(sSql),
vntRecs,
adCmdText);
}
return lRecs;
}
// DBInitTable() 只需要运行一次即可,把test()的改一下并运行一次就好。
int CStkLibWnd::DBInitTable()
{
CSPString sSql = “”;
int nflase =0;
//1. 证券代码表
sSql= “CREATE TABLE STK_STOCK( \n”
“m_wMarket [varchar](8), \n”
“m_szLabel Varchar(10), \n”
“m_szName Varchar(32), \n”
“m_szPingYing [varchar](10), \n”
“m_nUnit numeric(4), \n”
“m_nDigit numeric(4), \n”
“m_fLastClose [numeric](18,6), \n”
“[m_time] [datetime] ) \n”;
nflase += CnnExecuteSql(sSql, true);
//2. 实行行情
sSql= “CREATE TABLE STK_REPORT( \n”
“[m_cbSize] [varchar](4), \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10) ,\n”
“[m_szName] [varchar](30), \n”
“[m_time] [datetime] , \n”
“[m_dwFlag] [varchar](4), \n”
“[m_fLast] [numeric](18,6), \n”
“[m_fOpen] [numeric](18,6), \n”
“[m_fHigh] [numeric](18,6), \n”
“[m_fLow] [numeric](18,6), \n”
“[m_fNew] [numeric](18,6), \n”
“[m_fVolume] [numeric](18,6), \n”
“[m_fAmount] [numeric](18,6), \n”
“[m_fBuyPrice1] [numeric](18,6), \n”
“[m_fBuyVolume1] [numeric](18,6), \n”
“[m_fSellPrice1] [numeric](18,6), \n”
“[m_fSellVolume1] [numeric](18,6), \n”
“[m_fBuyPrice2] [numeric](18,6), \n”
“[m_fBuyVolume2] [numeric](18,6), \n”
“[m_fSellPrice2] [numeric](18,6), \n”
“[m_fSellVolume2] [numeric](18,6), \n”
“[m_fBuyPrice3] [numeric](18,6), \n”
“[m_fBuyVolume3] [numeric](18,6), \n”
“[m_fSellPrice3] [numeric](18,6), \n”
“[m_fSellVolume3] [numeric](18,6), \n”
“[m_fBuyPrice4] [numeric](18,6), \n”
“[m_fBuyVolume4] [numeric](18,6), \n”
“[m_fSellPrice4] [numeric](18,6), \n”
“[m_fSellVolume4] [numeric](18,6), \n”
“[m_fBuyPrice5] [numeric](18,6), \n”
“[m_fBuyVolume5] [numeric](18,6), \n”
“[m_fSellPrice5] [numeric](18,6), \n”
“[m_fSellVolume5] [numeric](18,6), \n”
“[m_fAvgPrice] [numeric](18,6), \n”
“[m_dwTickAll] [varchar](4)) \n”;
nflase += CnnExecuteSql(sSql, true);
//3. 分笔数据(盘口) 主表
sSql= “CREATE TABLE STK_TICK_HDR ( \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10), \n”
“[m_lDate] DateTime, \n”
“[m_fLastClose] [numeric](18,6), \n”
“[m_fOpen] [numeric](18,6), \n”
“[m_nAllCount] [numeric](18,6), \n”
“[m_nCount] [numeric](18,6)) \n”;
nflase += CnnExecuteSql(sSql, true);
//4. 分笔数据(盘口) 从表
sSql= “CREATE TABLE STK_TICK( \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10) ,\n”
“[m_szName] [varchar](30), \n”
“[m_time] DateTime, \n”
“[m_lTime] DateTime, \n”
“[m_fHigh] [numeric](18,6), \n”
“[m_fLow] [numeric](18,6), \n”
“[m_fNewPrice] [numeric](18,6), \n”
“[m_fVolume] [numeric](18,6), \n”
“[m_fAmount] [numeric](18,6), \n”
“[m_lStroke] [numeric](18,6), \n”
“[m_fBuyPrice1] [numeric](18,6), \n”
“[m_fBuyVolume1] [numeric](18,6), \n”
“[m_fSellPrice1] [numeric](18,6), \n”
“[m_fSellVolume1] [numeric](18,6), \n”
“[m_fBuyPrice2] [numeric](18,6), \n”
“[m_fBuyVolume2] [numeric](18,6), \n”
“[m_fSellPrice2] [numeric](18,6), \n”
“[m_fSellVolume2] [numeric](18,6), \n”
“[m_fBuyPrice3] [numeric](18,6), \n”
“[m_fBuyVolume3] [numeric](18,6), \n”
“[m_fSellPrice3] [numeric](18,6), \n”
“[m_fSellVolume3] [numeric](18,6), \n”
“[m_fBuyPrice4] [numeric](18,6), \n”
“[m_fBuyVolume4] [numeric](18,6), \n”
“[m_fSellPrice4] [numeric](18,6), \n”
“[m_fSellVolume4] [numeric](18,6), \n”
“[m_fBuyPrice5] [numeric](18,6), \n”
“[m_fBuyVolume5] [numeric](18,6), \n”
“[m_fSellPrice5] [numeric](18,6), \n”
“[m_fSellVolume5] [numeric](18,6))\n”;
nflase += CnnExecuteSql(sSql, true);
//5. 补充除权数据
sSql= “CREATE TABLE STK_POWER ( \n”
“[m_dwHeadTag] [varchar](4), \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10), \n”
“[m_time] DateTime, \n”
“[m_fGive] [numeric](18,6), \n”
“[m_fPei] [numeric](18,6), \n”
“[m_fPeiPrice] [numeric](18,6), \n”
“[m_fProfit] [numeric](18,6), \n”
“[m_dwReserved] [varchar](4)) \n”;
nflase += CnnExecuteSql(sSql, true);
//6. 分时数据
sSql= “CREATE TABLE STK_KDATA_MIN (\n”
“[m_dwHeadTag] [varchar](4), \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10), \n”
“[m_date] DateTime, \n”
“[m_time] DateTime, \n”
“[m_fOpen] [numeric](18,6), \n”
“[m_fHigh] [numeric](18,6), \n”
“[m_fLow] [numeric](18,6), \n”
“[m_fClose] [numeric](18,6), \n”
“[m_fVolume] [numeric](18,6), \n”
“[m_fAmount] [numeric](18,6), \n”
“[m_dwAdvance] [varchar](4), \n”
“[m_dwDecline] [varchar](4), \n”
“[m_dwReserved] [varchar](4)) \n”;
nflase += CnnExecuteSql(sSql, true);
//7. 日线数据表
sSql= “CREATE TABLE STK_KDATA_DAY( \n”
“[m_dwHeadTag] [varchar](4), \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10), \n”
“[m_time] DateTime, \n”
“[m_fOpen] [numeric](18,6), \n”
“[m_fHigh] [numeric](18,6), \n”
“[m_fLow] [numeric](18,6), \n”
“[m_fClose] [numeric](18,6), \n”
“[m_fVolume] [numeric](18,6), \n”
“[m_fAmount] [numeric](18,6), \n”
“[m_wAdvance] [varchar](4), \n”
“[m_wDecline] [varchar](4)) \n”;
nflase += CnnExecuteSql(sSql, true);
//8. 1分钟数据线
sSql= “CREATE TABLE STK_KDATA_MIN1 (\n”
“[m_dwHeadTag] [varchar](4), \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10), \n”
“[m_date] DateTime, \n”
“[m_time] DateTime, \n”
“[m_fOpen] [numeric](18,6), \n”
“[m_fHigh] [numeric](18,6), \n”
“[m_fLow] [numeric](18,6), \n”
“[m_fClose] [numeric](18,6), \n”
“[m_fVolume] [numeric](18,6), \n”
“[m_fAmount] [numeric](18,6), \n”
“[m_dwAdvance] [varchar](4), \n”
“[m_dwDecline] [varchar](4), \n”
“[m_dwReserved] [varchar](4)) \n”;
nflase += CnnExecuteSql(sSql, true);
//9. 5分钟数据线
sSql= “CREATE TABLE STK_KDATA_MIN5 ( \n”
“[m_dwHeadTag] [varchar](4), \n”
“[m_wMarket] [varchar](8), \n”
“[m_szLabel] [varchar](10), \n”
“[m_date] DateTime, \n”
“[m_time] DateTime, \n”
“[m_fOpen] [numeric](18,6), \n”
“[m_fHigh] [numeric](18,6), \n”
“[m_fLow] [numeric](18,6), \n”
“[m_fClose] [numeric](18,6), \n”
“[m_fVolume] [numeric](18,6), \n”
“[m_fAmount] [numeric](18,6), \n”
“[m_dwAdvance] [varchar](4), \n”
“[m_dwDecline] [varchar](4), \n”
“[m_dwReserved] [varchar](4)) \n”;
nflase += CnnExecuteSql(sSql, true);
//10. 操作记录表
sSql= “CREATE TABLE STK_LOG ( \n”
” m_date DateTime, \n”
” m_Updated [varchar](10), \n”
” m_nCount [numeric](8), \n”
” m_wMarket [varchar](10), \n”
” m_szLabel [varchar](10), \n”
” m_Type [varchar](30)) \n”;
nflase += CnnExecuteSql(sSql, true);
//11. 基本信息表
sSql= “create table basedata( \n”
“m_dwMarket varchar(8),\n” // DWORD ; 证券市场类型,see CStock::StockMarket
“m_szCode varchar(8),\n” // char m_szCode[STKLIB_MAX_CODE2]; // 证券代码,以’\0’结尾
“m_szDomain varchar(20),\n” // char m_szDomain[STKLIB_MAX_DOMAIN]; // 板块
“m_szProvince varchar(20),\n” // char m_szProvince[STKLIB_MAX_PROVINCE]; // 省份
“m_time datetime,\n” // time_t m_time; // 日期,time_t
“m_date datetime,\n” // DWORD m_date; // 日期 Format is YYYYMMDD for base data
“m_reporttype numeric(18,6),\n” // LONG m_reporttype; // 报告类型:年报、中报、季报
“m_fErate_dollar numeric(18,6),\n” // float m_fErate_dollar; // 当期美元汇率
“m_fErate_hkdollar numeric(18,6),\n” //float m_fErate_hkdollar; // 当期港币汇率 –// ★偿债能力
“m_fRatio_liquidity numeric(18,6),\n” // float m_fRatio_liquidity; // 流动比率
“m_fRatio_quick numeric(18,6),\n” // float m_fRatio_quick; // 速动比率
“m_fVelocity_receivables numeric(18,6),\n” // float m_fVelocity_receivables; // 应收帐款周率–// ★经营能力
“m_fVelocity_merchandise numeric(18,6),\n” // float m_fVelocity_merchandise; // 存货周转率
“m_fMain_income numeric(18,6),\n” // float m_fMain_income; // 主营业务收入
“m_fCash_ps numeric(18,6),\n” // float m_fCash_ps; // 每股净现金流量–// ★盈利能力
“m_fProfit_margin numeric(18,6),\n” // float m_fProfit_margin; // 主营业务利润率 %
“m_fNetasset_yield numeric(18,6),\n” // float m_fNetasset_yield; // 净资产收益率 %
“m_datebegin datetime,\n” // DWORD m_datebegin; // 上市日期 Format is YYYYMMDD-// ★资本结构
“m_fShare_count_total varchar(20),\n” // float m_fShare_count_total; // 总股本
“m_fShare_count_a varchar(20),\n” // float m_fShare_count_a; // 流通A股
“m_fShare_count_b varchar(20),\n” //float m_fShare_count_b; // 流通B股
“m_fShare_count_h varchar(20),\n” //float m_fShare_count_h; // 流通H股
“m_fShare_count_national varchar(20),\n” //float m_fShare_count_national; // 国有股
“m_fShare_count_corp varchar(20),\n” //float m_fShare_count_corp; // 法人股
“m_fProfit_psud varchar(20),\n” //float m_fProfit_psud; // 每股未分配利润
“m_fAsset varchar(20),\n” //float m_fAsset; // 总资产
“m_fRatio_holderright varchar(20),\n” //float m_fRatio_holderright; // 股东权益比率
“m_fRatio_longdebt varchar(20),\n” //float m_fRatio_longdebt; // 长期负债率
“m_fRatio_debt varchar(20),\n” //float m_fRatio_debt; // 资产负债率–// ★投资收益能力
“m_fNetasset_ps varchar(20),\n” //float m_fNetasset_ps; // 每股净资产
“m_fNetasset_ps_regulate varchar(20),\n” //float m_fNetasset_ps_regulate; // 调整每股净资产
“m_fEps varchar(20),\n” //float m_fEps; // 每股收益
“m_fEps_deduct varchar(20),\n” //float m_fEps_deduct; // 扣除后每股收益
“m_fNet_profit varchar(20),\n” //float m_fNet_profit; // 净利润
“m_fMain_profit varchar(20),\n” //float m_fMain_profit; // 主营业务利润
“m_fTotal_profit varchar(20),\n” //float m_fTotal_profit; // 利润总额–// ★增长率
“m_fProfit_inc varchar(20),\n” //float m_fProfit_inc; // 主营利润增长率%
“m_fIncome_inc varchar(20),\n” //float m_fIncome_inc; // 主营收入增长率%
“m_fAsset_inc varchar(20),\n” //float m_fAsset_inc; // 总资产增长率%” –// ★技术数据统计值
“m_fYield_average varchar(20),\n” //float m_fYield_average; // 平均收益率%
“m_fYield_stddev varchar(20),\n” //float m_fYield_stddev; // 收益标准差%
“m_fBeite varchar(20),\n” //float m_fBeite; // β值
“m_dwReserved1 varchar(8),\n” // DWORD m_dwReserved[4];
“m_dwReserved2 varchar(8),\n” //
“m_dwReserved3 varchar(8),\n” //
“m_dwReserved4 varchar(8),\n” //
“) \n”; //
nflase += CnnExecuteSql(sSql, true);
sSql.Format(“insert into STK_LOG (m_date, m_Updated, m_nCount, m_wMarket, m_szLabel, m_Type)”
“select GETDATE(),’SYSTEM’, %d, ”,”,’连接,更新数据表'”,nflase);
nflase += CnnExecuteSql(sSql,true);
return nflase;
}