设为首页 收藏本站
查看: 432|回复: 0

[经验分享] visual c++ 2008进行MySQL编程(ODBC) --三 查询数据库

[复制链接]

尚未签到

发表于 2016-10-23 02:32:27 | 显示全部楼层 |阅读模式
  前面两讲说到了,安装MySql数据库,安装ODBC驱动以及使用CDatabase操作数据库的基本操作比如Add del edit,链接如下:

  visual c++ 2008进行MySQL编程(ODBC) -- (一) 套装安装
  visual c++ 2008进行MySQL编程(ODBC) --二 操作数据库
  visual c++ 2008进行MySQL编程(ODBC) --三 查询数据库
  visual c++ 2008进行MySQL编程(ODBC)-- (四) 终极实现 之 派生CRecordset 上

  visual c++ 2008进行MySQL编程(ODBC)-- (四) 终极实现 之 派生CRecordset 中
  今天引入一个新的类,CRecordset,其实前面的编程操作对于CRecordset类而言,相对而言就非常的弱小了呵呵,后续一一说到吧。今天的工作就是使用这个类遍历一下我们数据库里面的条目。
  继续在上一个文章的工程上做这一切。
  
  一、在对话框里面托一个listtree控件,修改属性为报表模式:
DSC0000.jpg
  上图的黄色的框框的属性要修改report,报表格式。
  二、给这个新加的list control添加成员变量,m_list_ctrl:
DSC0001.jpg
  
  三、在Cmy_dbDlg类的初始化函数OnInitDialog()里面添加添加的list control的初始化代码:

    m_list_ctrl.InsertColumn( 0, _T("Cust Id"), LVCFMT_CENTER, 70 );//插入列
m_list_ctrl.InsertColumn( 1, _T("Cust Name"), LVCFMT_CENTER, 85 );
  
  四、添加一个按钮,用作查询,同时给这个按钮添加Cmy_dbDlg的单击响应函数:
DSC0002.jpg
  查询的按钮就是上图的GetRecord。
  消息响应函数为:

void Cmy_dbDlg::OnBnClickedGetAllItem()
{
// TODO: Add your control notification handler code here
}

  
四、现在来实现这个消息函数,我们使用类CRecordset,具体的要使用的函数,我们看看MSDN的文档:

Opens the recordset by retrieving the table or performing the query that the recordset represents.
virtual BOOL Open(
UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE,
LPCTSTR lpszSQL = NULL,
DWORD dwOptions = none  
);
Returns nonzero if the recordset has been positioned before the first record. There is no current record.
BOOL IsBOF( ) const;
Returns nonzero if the recordset has been positioned after the last record. There is no current record.
BOOL IsEOF( ) const;
Makes the first record in the next rowset the current record.
void MoveNext( );

  
Open函数打开指定的数据库,之后才可以遍历,如果数据库为空,则IsBOF返回true,如果需要遍历每一条,则使用MoveNext( )。
  使用起来就是这么简单。
  五、OnBnClickedGetAllItem()的实现修改如下:

void Cmy_dbDlg::OnBnClickedGetAllItem()
{
// TODO: Add your control notification handler code here
CRecordset my_record(&m_db_opr);
try
{
int count = 0;
CString str;
my_record.Open(CRecordset::snapshot, _T("select * from customer"));
if(my_record.IsBOF())
{
return;
}
while(!my_record.IsEOF())
{
my_record.GetFieldValue((short)0, str);
m_list_ctrl.InsertItem(count, str);
my_record.GetFieldValue(1, str);
m_list_ctrl.SetItemText(count, 1, str);
my_record.MoveNext();
count++;
}
}
catch(CDBException* pe)
{
// The error code is in pe->m_nRetCode
pe->ReportError();
pe->Delete();
}
}

  
现在解释一下上面的代码吧,Open函数还是请大家参考一下MSDN呵呵,第一个参数可以为:

  CRecordset::dynaset A recordset with bi-directional scrolling. The membership and ordering of the records are determined when the recordset is opened, but changes made by other users to the data values are visible following a fetch operation.
Dynasets are also known as keyset-driven recordsets.


  CRecordset::snapshot A static recordset with bi-directional scrolling. The membership and ordering of the records are determined when the recordset is opened; the data values are determined when the records are fetched. Changes made by other
users are not visible until the recordset is closed and then reopened.


  CRecordset::dynamic A recordset with bi-directional scrolling. Changes made by other users to the membership, ordering, and data values are visible following a fetch operation. Note that many ODBC drivers do not support this type of recordset.
  CRecordset::forwardOnly A read-only recordset with only forward scrolling.
  For CRecordset, the default value is CRecordset::snapshot. The default-value mechanism allows the Visual C++ wizards to interact with both ODBCCRecordset and DAOCDaoRecordset, which have
different defaults.
  具体我就不翻译了,mysql的ODBC貌似只支持snapshot,就是快照,静态的获取当前的数据库里面的条目数。
  Open的第二个参数,是一个字符串,就是用于查询数据库的的sql语句。
  编译代码,运行,我们执行一下,单击GetRecord按钮就能查询到当前的所有条目,如图:
DSC0003.jpg
  挺完美对吧。
  还没有结束,还要继续说说如何设定查询范围,设定排序方式等等。
  

  六、我们设定过滤规则,比如我想要查询100<= x <= 200范围内的cust id的条目,如何进行?
  解决这个问题需要求助于
  m_strFilter
  这个成员变量了,所以我们在Open函数调用之前,设定查询范围,如下:

        my_record.m_strFilter = _T("cust_id <= 200 and cust_id >= 100");
my_record.Open(CRecordset::snapshot, _T("select * from customer"));
  
那么修改后面的OnBnClickedGetAllItem函数如下:

void Cmy_dbDlg::OnBnClickedGetAllItem()
{
// TODO: Add your control notification handler code here
CRecordset my_record(&m_db_opr);
try
{
int count = 0;
CString str;
my_record.m_strFilter = _T("cust_id <= 200 and cust_id >= 100");
my_record.Open(CRecordset::snapshot, _T("select * from customer"));
if(my_record.IsBOF())
{
return;
}
while(!my_record.IsEOF())
{
my_record.GetFieldValue((short)0, str);
m_list_ctrl.InsertItem(count, str);
my_record.GetFieldValue(1, str);
m_list_ctrl.SetItemText(count, 1, str);
my_record.MoveNext();
count++;
}
}
catch(CDBException* pe)
{
// The error code is in pe->m_nRetCode
pe->ReportError();
pe->Delete();
}
}


  
我们可以看到,查询结果如下图:
DSC0004.jpg
  但是我们知道,设定查询范围怎么弄?需要where 子句,比如 :
  select * from customer where cust_id <= 200 and cust_id >= 100;
  但是使用了m_strFilter这个作为过滤规则的设定,就不能使用带上where了。

  

  七、我们可以设定排序规则,比如按照cust id排序,而且是逆序,这个操作就要借助于
  m_strSort
  了,也只要设定排序对象就是了:
  

my_record.m_strFilter = _T("cust_id <= 200 and cust_id >= 100");
my_record.m_strSort = _T("cust_id desc");
my_record.Open(CRecordset::snapshot, _T("select * from customer"));

  


和上面过滤的规则一样,sql语句是需要Order by子句的,如果使用了m_strSort之后,那么就不能有“order by”了,而这个语句:

my_record.m_strSort = _T("cust_id desc");



就相当于, “order by cust_id desc” 子句了。


修改后的消息响应函数就是这个样子的了:  


  

  

void Cmy_dbDlg::OnBnClickedGetAllItem()
{
// TODO: Add your control notification handler code here
CRecordset my_record(&m_db_opr);
try
{
int count = 0;
CString str;
my_record.m_strFilter = _T("cust_id <= 200 and cust_id >= 100");
my_record.m_strSort = _T("cust_id desc");
my_record.Open(CRecordset::snapshot, _T("select * from customer"));
if(my_record.IsBOF())
{
return;
}
while(!my_record.IsEOF())
{
my_record.GetFieldValue((short)0, str);
m_list_ctrl.InsertItem(count, str);
my_record.GetFieldValue(1, str);
m_list_ctrl.SetItemText(count, 1, str);
my_record.MoveNext();
count++;
}
}
catch(CDBException* pe)
{
// The error code is in pe->m_nRetCode
pe->ReportError();
pe->Delete();
}
}

  
  
  
  
  


执行一下程序,自然就逆序打印了:

DSC0005.jpg

是不是很不错啊?

还是那句话,如果有下一个博文,我会继续说更好的使用visual c++ 2008对mysql数据库编程的实现。


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-289851-1-1.html 上篇帖子: 转:mysql的索引使用不当速度比没加索引还慢 下篇帖子: INSERT 中ON DUPLICATE KEY UPDATE和mysql replace into的使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表